我用一种分类法定制了post类型。除了分类学上的分页外,一切都很好。首先,在没有任何更改的情况下,我能够在主自定义帖子类型存档上切换页面:
website.com/custom_post_type_name/page/x
但当我想跳转到分类法并切换页面时,我遇到了404错误。
website.com/custom_post_type_name/taxonomy_slug/page/x
我试着从StackExchange的answer中使用正则表达式,但根本不起作用。
add_filter( \'rewrite_rules_array\', \'my_insert_rewrite_rules\' );
function my_insert_rewrite_rules( $rules ) {
$newrules = array();
$newrules[\'promocje(/[a-z]+)(/page/([0-9]+))?\'] =
\'index.php?post_type=promocje\'
. \'&shop=$matches[1]&paged=$matches[3]\';
return $newrules + $rules;
}
promocje
是帖子类型的名称shop
是分类法的名称在那之后,我可以在分类法页面上切换页面,但在为自定义帖子类型分页时出现404错误。自定义帖子类型和分类代码:
$args = array(
\'labels\' => $labels,
\'hierarchical\' => false,
\'description\' => \'Promocje filtrowane przez sklep\',
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'trackbacks\', \'custom-fields\', \'comments\', \'page-attributes\' ),
\'taxonomies\' => array( \'shop\'),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'menu_icon\' => \'dashicons-format-audio\',
\'show_in_nav_menus\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'query_var\' => true,
\'can_export\' => true,
\'capability_type\' => \'post\',
\'register_meta_box_cb\' => \'add_promo_metaboxes\',
\'rewrite\' => array( \'slug\' => \'promocje/%shop%\', \'with_front\' => false ),
\'has_archive\' => \'promocje\',
);
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == \'promocje\' ){
$terms = wp_get_object_terms( $post->ID, \'shop\' );
if( $terms ){
return str_replace( \'%shop%\' , $terms[0]->slug , $post_link);
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'wpa_show_permalinks\', 1, 2 );
和分类:register_taxonomy(
\'shop\',
\'promocje\',
array(
\'hierarchical\' => true,
\'label\' => \'Sklep\',
\'query_var\' => true,
\'rewrite\' => array(
\'slug\' => \'promocje\',
\'with_front\' => false
)
)
);
任何帮助都将不胜感激。我已经试着解决这个问题一个多星期了。
最合适的回答,由SO网友:kaska3er 整理而成
我解决了!您需要在第1页和404页上var\\u dump wp\\u查询
add_action( \'pre_get_posts\', \'elo_jlb\' );
function elo_jlb($query)
{
if(!is_admin())
{
$query->set(\'paged\', $query->get(\'page\'));
$query->set(\'HERE_POST_TYPE_NAME\', \'\');
}
}