自定义分类归档返回404

时间:2016-10-03 作者:kaska3er

我用一种分类法定制了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 是分类法的名称

    自定义帖子类型和分类代码:

        $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
            )
        )
    );
    
    任何帮助都将不胜感激。我已经试着解决这个问题一个多星期了。

2 个回复
最合适的回答,由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\', \'\');
    }
}

SO网友:Zaid Sameer

从管理仪表板,转到Permalink Settings 然后击中Save Changes. 希望这能解决问题。

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?