我有一个名为“推荐信”的自定义帖子类型,其中包含reviews
. 我还有一个名为“推荐部门”的分类法reviews
. 目标是我将拥有像/reviews/sales/
或/reviews/service/
.
除了分页之外,一切都按照我的意愿进行。我尝试了所有不同的提示和技巧,以使分页按预期工作,但不管怎样/reviews/sales/page/2
就是不管用。但是,如果我手动输入/reviews/sales/?page=2
正如人们所期望的那样,这将导致第二页的结果。是否有允许我使用查询字符串的重写规则?page=2
但重写为/page/2/
或者有没有办法强制WordPress使用带有页码的非漂亮永久链接作为具有正常分页的查询字符串?
这就是我的分类法和自定义帖子类型注册的样子,另外,我还有一个帖子重写规则。
add_action( \'init\', \'create_testimonial_department_taxonomy\' );
function create_testimonial_department_taxonomy() {
$labels = array(
\'name\' => \'Testimonial Department\',
\'singular_name\' => \'Testimonial Department\',
\'search_items\' => \'Search Testimonial Departments\',
\'all_items\' => \'All Testimonial Departments\',
\'edit_item\' => \'Edit Testimonial Department\',
\'update_item\' => \'Update Testimonial Department\',
\'add_new_item\' => \'Add New Testimonial Department\',
\'new_item_name\' => \'New Testimonial Department\',
\'menu_name\' => \'Testimonial Department\',
\'view_item\' => \'View Testimonial Department\',
\'not_found\' => \'No Testimonial Departments found\'
);
register_taxonomy(
\'testimonial-department\',
\'testimonials\',
array(
\'label\' => __( \'Testimonial Departments\' ),
\'hierarchical\' => false,
\'labels\' => $labels,
\'public\' => true,
\'show_in_nav_menus\' => true,
\'meta_box_cb\' => false,
\'show_tagcloud\' => false,
\'show_admin_column\' => true,
\'query_var\' => true,
\'sort\' => true,
\'rewrite\' => array(
\'slug\' => \'reviews\',
\'hierarchical\' => false
)
)
);
}
function aw_testimonials_post_type() {
// Set UI labels for Testinomials post type
$labels = array(
\'name\' => _x( \'Testimonials\', \'Post Type General Name\'),
\'singular_name\' => _x( \'Testimonial\', \'Post Type Singular Name\'),
\'menu_name\' => __( \'Testimonials\'),
\'parent_item_colon\' => __( \'Testimonials Parent\'),
\'all_items\' => __( \'All Testimonials\'),
\'view_item\' => __( \'View Testimonial\'),
\'add_new_item\' => __( \'Add New Testimonial\'),
\'add_new\' => __( \'Add New\'),
\'edit_item\' => __( \'Edit Testimonial\'),
\'update_item\' => __( \'Update Testimonial\'),
\'search_items\' => __( \'Search Testimonial\'),
\'not_found\' => __( \'Testimonial Not Found\'),
\'not_found_in_trash\' => __( \'Testimonial Not found in Trash\'),
);
// Set other options for Testimonials post type
$args = array(
\'label\' => __( \'Testimonials\'),
\'description\' => __( \'Posts specifically for displaying testimonials.\'),
\'labels\' => $labels,
\'rewrite\' => array(\'slug\' => \'reviews/%testimonial-department%\'),
\'query_var\' => true,
\'menu_icon\' => \'dashicons-megaphone\',
\'supports\' => array(\'title\',\'thumbnail\'),
\'taxonomies\' => array(\'post_tag\', \'testimonial-department\'),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'menu_position\' => 14,
\'can_export\' => true,
\'has_archive\' => \'reviews\',
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
);
// Registering your Custom Post Type
register_post_type( \'testimonials\', $args );
}
add_action( \'init\', \'aw_testimonials_post_type\', 0 );
function testimonial_department_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, \'testimonial-department\' );
if( $terms ){
return str_replace( \'%testimonial-department%\' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'testimonial_department_post_link\', 1, 3 );
function testimonials_posts_per_page( $query ) {
if (is_admin()) {
return $query;
}
if ( $query->is_main_query() ) {
if ( is_post_type_archive( \'testimonials\' ) ) {
$query->set( \'posts_per_page\', 6 );
return $query;
}
else if ( is_tax( \'testimonial-department\' ) ) {
$query->set( \'posts_per_page\', 6 );
$query->set(\'post_type\',\'testimonials\');
return;
}
}
}
add_action( \'pre_get_posts\', \'testimonials_posts_per_page\', 1 );