doumen5895 2018-05-30 05:59
浏览 35

自定义分类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

  • 写回答

1条回答 默认 最新

  • du1068 2018-05-30 06:36
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看