用于定制分页的漂亮URL

时间:2016-12-24 作者:gpcola

我有一个带有自定义分页的自定义查询,我希望使用漂亮的url,比如标准分页,而不是将查询字符串附加到url,即

www.example。com/posttype/pagename/cpage/2

而不是

www.example。com/posttype/pagename/?cpage=2

使用paginate\\u links()的format参数创建漂亮的URL非常简单

$args = array(
    \'format\'    => \'cpage/%#%\',
    \'current\'   => $cpage,
    \'total\'     => $custom_query->max_num_pages,
    \'type\'      => \'array\',
    \'prev_text\' => __(\'Previous\'),
    \'next_text\' => __(\'Next\'),
);
$paging = paginate_links( $args );
我遇到的问题是,当访问页面时,需要将这些url重写回查询字符串,因为WP无法识别漂亮url中的“cpage”变量。

所以我在中重写URL。htaccess这样

RewriteRule ^/posttype/([^/]*)/cpage/([0-9]*) /posttype/$1/?cpage=$2 [QSA]
我已经验证了这个规则应该重写我期望使用的方式http://martinmelin.se/rewrite-rule-tester/. 将规则应用于时。htaccess在我的网站上,但我在访问分页链接时得到了404,我不明白这是为什么。如果我不使用漂亮的URL,分页就可以正常工作。

过去我多次使用mod\\u rewrite,但从未使用WP。我是否应该意识到任何可能会影响我尝试做的事情的陷阱,或者是否有更好的方法来实现这一点?

使用Post类型注册Toolset Types plugin:Post type registration using Toolset Types plugin

get_post_type_object

WP_Post_Type Object
(
    [name] => deal
    [label] => Deals
    [labels] => stdClass Object
        (
            [name] => Deals
            [singular_name] => Deal
            [add_new] => Add New
            [add_new_item] => Add New Deal
            [edit_item] => Edit Deal
            [new_item] => New Deal
            [view_item] => View Deal
            [search_items] => Search Deals
            [not_found] => No Deals found
            [not_found_in_trash] => No Deals found in Trash
            [parent_item_colon] => Parent text
            [all_items] => All items
            [archives] => All items
            [insert_into_item] => Insert into post
            [uploaded_to_this_item] => Uploaded to this post
            [featured_image] => Featured Image
            [set_featured_image] => Set featured image
            [remove_featured_image] => Remove featured image
            [use_featured_image] => Use as featured image
            [filter_items_list] => Filter posts list
            [items_list_navigation] => Posts list navigation
            [items_list] => Posts list
            [menu_name] => Deals
            [enter_title_here] => Enter title here
            [name_admin_bar] => Deal
        )

    [description] => The link between compare and operator
    [hierarchical] => 
    [exclude_from_search] => 1
    [publicly_queryable] => 1
    [show_ui] => 1
    [show_in_menu] => 1
    [show_in_nav_menus] => 1
    [show_in_admin_bar] => 1
    [menu_position] => 
    [menu_icon] => dashicons-admin-post
    [capability_type] => post
    [map_meta_cap] => 1
    [register_meta_box_cb] => 
    [taxonomies] => Array
        (
        )

    [has_archive] => 
    [query_var] => deal
    [can_export] => 1
    [delete_with_user] => 
    [_builtin] => 
    [_edit_link] => post.php?post=%d
    [cap] => stdClass Object
        (
            [edit_post] => edit_post
            [read_post] => read_post
            [delete_post] => delete_post
            [edit_posts] => edit_posts
            [edit_others_posts] => edit_others_posts
            [publish_posts] => publish_posts
            [read_private_posts] => read_private_posts
            [read] => read
            [delete_posts] => delete_posts
            [delete_private_posts] => delete_private_posts
            [delete_published_posts] => delete_published_posts
            [delete_others_posts] => delete_others_posts
            [edit_private_posts] => edit_private_posts
            [edit_published_posts] => edit_published_posts
            [create_posts] => edit_posts
        )

    [rewrite] => Array
        (
            [enabled] => 1
            [with_front] => 1
            [pages] => 1
            [feeds] => 
            [slug] => deal
            [ep_mask] => 1
        )

    [_toolset_edit_last] => 1482798189
    [_wpcf_author_id] => 1
    [wpcf-post-type] => deal
    [icon] => admin-post
    [slug] => deal
    [dashboard_glance] => 1
    [custom-field-group] => Array
        (
            [5] => 1
        )

    [has_archive_slug] => 
    [show_in_menu_page] => 
    [query_var_enabled] => 1
    [permalink_epmask] => 1
    [rest_base] => deal
    [post_relationship] => Array
        (
            [belongs] => Array
                (
                    [compare] => 1
                    [operator] => 1
                )

        )

    [show_in_rest] => 
)
rewrite analyzer plugin outputenter image description here

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您可以通过WordPress的内部重写系统adding a rewrite endpoint:

function wpd_cpage_endpoint() {
    add_rewrite_endpoint( \'cpage\', EP_PERMALINK );
}
add_action( \'init\', \'wpd_cpage_endpoint\' );
这将添加额外的重写规则,以启用/cpage/n/ 在permalinks的末尾。

然后,您可以使用get_query_var( \'cpage\' ).

编辑-

这是我用来注册post类型和端点的代码:

function wpd_test_post_type() {

    $args = array(
        \'label\' => \'Test Type\',
        \'public\' => true,
        \'rewrite\' => true,
        \'supports\' => array( \'title\' ),
    );

    register_post_type(
        \'test_type\',
        $args
    );

    add_rewrite_endpoint( \'cpage\', EP_PERMALINK );

}
add_action( \'init\',\'wpd_test_post_type\' );

相关推荐

Can't Add Pagination

我无法添加分页来在页面之间划分帖子并在页面之间导航。目前所有20篇帖子我都有一次加载。我想在我的页面底部添加分页,并在5之前查看帖子。我有一个自定义的帖子类型,我将其定义为Project。我有两页。主页和日志。主页以我的项目帖子类型为特征,日志以我的常规博客帖子为特征。我希望这两页都有分页。我的当前索引。php是我的主页,如下所示:<?php get_header(); ?> <?php get_footer(); ?> <div class=\"p