最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove custom taxonamy slug with pagination

programmeradmin1浏览0评论

I have added the custom taxonamy for my faq site. I want to remove the default custom taxonamy showing in url for archive page. I have tried some codes, but it's break my pagination.

   function my_taxonomies_faqs() {
  $labels = array(
    'name'              => _x( 'FAQ Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'FAQ Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search FAQ Categories' ),
    'all_items'         => __( 'All FAQ Categories' ),
    'parent_item'       => __( 'Parent FAQ Category' ),
    'parent_item_colon' => __( 'Parent FAQ Category:' ),
    'edit_item'         => __( 'Edit FAQ Category' ), 
    'update_item'       => __( 'Update FAQ Category' ),
    'add_new_item'      => __( 'Add New FAQ Category' ),
    'new_item_name'     => __( 'New FAQ Category' ),
    'menu_name'         => __( 'FAQ Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'hierarchical' => true,
    'show_in_rest' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'faq-categories', 'hierarchical' => true, 'with_front'=> false),    
  ); 
  register_taxonomy( 'faq-category', 'faq-post', $args );
}

Default URL Format:

www.example/faq/custom-tax/parent-cat/ 

I want to show like this below URL format:

 - www.example/faq - Homepage
 - www.example/faq/parent-cat/ - Level 1
 - www.example/faq/parent-cat/page/2 
 - www.example/faq/parent-cat/child-cat - level 2
 - www.example/faq/parent-cat/child-cat/page/2

Already i have tried this solution. But my child category pagination is shows the 404 Error. Link - .html

Any possible way to remove the default custom taxonamy in URL with pagination support?

I have added the custom taxonamy for my faq site. I want to remove the default custom taxonamy showing in url for archive page. I have tried some codes, but it's break my pagination.

   function my_taxonomies_faqs() {
  $labels = array(
    'name'              => _x( 'FAQ Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'FAQ Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search FAQ Categories' ),
    'all_items'         => __( 'All FAQ Categories' ),
    'parent_item'       => __( 'Parent FAQ Category' ),
    'parent_item_colon' => __( 'Parent FAQ Category:' ),
    'edit_item'         => __( 'Edit FAQ Category' ), 
    'update_item'       => __( 'Update FAQ Category' ),
    'add_new_item'      => __( 'Add New FAQ Category' ),
    'new_item_name'     => __( 'New FAQ Category' ),
    'menu_name'         => __( 'FAQ Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'hierarchical' => true,
    'show_in_rest' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'faq-categories', 'hierarchical' => true, 'with_front'=> false),    
  ); 
  register_taxonomy( 'faq-category', 'faq-post', $args );
}

Default URL Format:

www.example/faq/custom-tax/parent-cat/ 

I want to show like this below URL format:

 - www.example/faq - Homepage
 - www.example/faq/parent-cat/ - Level 1
 - www.example/faq/parent-cat/page/2 
 - www.example/faq/parent-cat/child-cat - level 2
 - www.example/faq/parent-cat/child-cat/page/2

Already i have tried this solution. But my child category pagination is shows the 404 Error. Link - https://rudrastyh/wordpress/remove-taxonomy-slug-from-urls.html

Any possible way to remove the default custom taxonamy in URL with pagination support?

Share Improve this question asked Mar 11, 2020 at 15:06 Rajkumar SRajkumar S 31 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

One of the general problems after creating new custom post type or taxonomy is showing 404 page after accessing to new urls that are generated by your custom post type or taxonomy.

In many of cases, the problem is related to flush_rewrite_rules(). After creating new urls, you have to do flush rewrite rules to rewriting urls by WordPress. If you don't use it, you will get 404 when you access to them.

So we have several option to solve this:

1. Simple way:

You can simply go to setting -> permalinks section in admin dashboard and then (without change any settings) click save changes button. With clicking save changes, WordPress core automatically call flush_rewrite_rules() in its process. Now check your urls and they are working.

2. Professional way:

Based on best practices in WordPress, it is better that you define your custom taxonomy and post types inside plugins.

So the best place to flush_rewrite_rules() is when the plugin is activated.

Notice: Please notice that you only need it only the first time that you define it. It is mentioned also in WordPress developer documentation of register_post_type and also in this link:

ATTENTION: This is *only* done during plugin activation hook in this example!
 You should *NEVER EVER* do this on every page load!!

You can see the sample of this, in my plugin boilerplate in github.

Note: If you want only to check it, you can use it after registering your taxonomy (only one time) and then check urls (then you can remove it), but if you are creating plugin which is used by many people, you have to do it when your plugin is activated.

I hope your problem is related to this simple trick and this way solves you problem.

发布评论

评论列表(0)

  1. 暂无评论