简体   繁体   中英

Permalink for Custom Taxonomy Wordpress

Currently, I have WordPress website which has pages, blog and custom post type named "Case Studies".

My permalink setting is a custom structure which is: /blog/%postname%/ so that I can show "Blog" in URLs for all blog posts.

URL structure for my custom post type is http://www.my-domain/case-studies/case-study-url-here/ which is working fine with below setup.

    "rewrite" => array('slug' => 'case-studies', 'with_front' => FALSE),

I have a custom taxonomy to categorize my case studies division wise, hence I have created a taxonomy called division and I want its URL to be " http://www.my-domain/case-studies/division-here/ but current it shows like " http://www.my-domain/blog/case-studies/division-here " which is not right.

To remove word blog from division URL, I changed rewrite code for the same to look like below.

    "rewrite" => array( 'slug' => 'case-studies', 'with_front' => false),

but the issue is, by doing this, I can not go to division taxonomy page properly it rather shows some random page or post from my website, but the old URL with "blog" base works fine and shows list of case studies on it if I make "with_front" true; but that is not how I want it.

Any help? how can I remove blog from URL and make it work?

Regards Manoj Soni

In your custom post type you need to add rewrite slug like 'rewrite' => array('slug' => '%division%', 'with_front' => false ),

and then you need to add following code in your function file.

function d_reset_permlinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'case-studies' ){
    $terms = wp_get_object_terms( $post->ID, 'division' );
    if( $terms ){
        return str_replace( '%division%' , $terms[0]->slug , $post_link );
    }
   }
   return $post_link;
}
 add_filter( 'post_type_link', 'd_reset_permlinks', 1, 2 );

And reset the permlinks with Post name then check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM