立即注册 找回密码

QQ登录

只需一步,快速开始

查看: 568|回复: 0

[Wordpress 通用教程] WordPress自定义文章类型和自定义分类共用同一个根路径slug的方法

[复制链接]
发表于 2023-12-2 10:21:38 | 显示全部楼层 |阅读模式
道勤网-数据www.daoqin.net

亲注册登录道勤网-可以查看更多帖子内容哦!(包涵精彩图片、文字详情等)请您及时注册登录-www.daoqin.net

您需要 登录 才可以下载或查看,没有账号?立即注册

x
wordpress中如果注册一个自定义文章类型(Custom Post Type),并且同时为这个类型注册一个自定义分类法(Custom Taxonomy),两者使用同一个根slug,访问这个类型的页面就会发生404报错。
这个slug具体指什么呢?举例说明:
  1. https://www.my-site.com/product/123.html
  2. https://www.my-site.com/product/term-name
复制代码
以上URL分别作为product类型页面的详情页和分类列表页,URL中的product就是根slug。在使用register_post_type注册product文章类型的时候,代码体现为:
  1. add_action('init', 'create_product_post_type', 0);
  2. function create_product_post_type() {       
  3.         register_post_type('product', array(
  4.                 'label' => 'Products',
  5.                 'public' => true,
  6.                 'show_ui' => true,
  7.                 'show_in_menu' => true,
  8.                 'capability_type' => 'post',
  9.                 'hierarchical' => false,
  10.                 'rewrite' => array('slug' => 'product'), //注意此行
  11.                 'supports' => array('title','editor','thumbnail'),
  12.                 'labels' => array (
  13.                         'name' => 'Products',
  14.                         'singular_name' => 'Product',
  15.                         'menu_name' => 'Products'
  16.                         )
  17.                 )
  18.         );
  19. }
复制代码
注意第11行的rewrite参数,就为此自定义类型指定了根slug,即形成如下url:
  1. https://www.my-site.com/product/123.html
复制代码
再来注册自定义分类法,这里的分类法名称为product_category:
  1. add_action( 'init', 'create_product_taxonomies', 0 );
  2. function create_product_taxonomies() {
  3.         register_taxonomy(
  4.                 'product_category',
  5.                 'product',
  6.                 array(
  7.                         'labels' => array(
  8.                                 'name' => 'Product Category'
  9.                         ),
  10.                         'rewrite' => array('slug' => 'product'),//注意此行
  11.                         'show_ui' => true,
  12.                         'show_tagcloud' => false,
  13.                         'hierarchical' => true
  14.                 )
  15.         );
  16. }
复制代码
注意第10行的rewrite参数,为此自定义分类指定了根slug,即形成如下url:
  1. https://www.my-site.com/product/term-name
复制代码
但实际上我们注册完毕后,前端页面会在自定义分类的URL产生404报错。
假如去掉第10行代码,默认的slug会变成product_category,即分类名称,这时候url会变成
  1. https://www.my-site.com/product_category/term-name
复制代码
这样虽然不会报错,但url比较难看,也不利于seo
要解决这个问题,需要在注册自定义类型的时候,修改参数如下:
  1. add_action('init', 'create_product_post_type', 0);
  2. function create_product_post_type() {       
  3.         register_post_type('product', array(
  4.                 'label' => 'Products',
  5.                 'public' => true,
  6.                 'show_ui' => true,
  7.                 'show_in_menu' => true,
  8.                 'capability_type' => 'post',
  9.                 'hierarchical' => false,
  10.                 'rewrite' => array('slug' => 'product/%product_category%'), //注意参数修改
  11.                 'has_archive' => 'product', //注意此行为添加的
  12.                 'supports' => array('title','editor','thumbnail'),
  13.                 'labels' => array (
  14.                         'name' => 'Products',
  15.                         'singular_name' => 'Product',
  16.                         'menu_name' => 'Products'
  17.                         )
  18.                 )
  19.         );
  20. }

  21. add_action( 'init', 'create_product_taxonomies', 0 );
  22. function create_product_taxonomies() {
  23.         register_taxonomy(
  24.                 'product_category',
  25.                 'product',
  26.                 array(
  27.                         'labels' => array(
  28.                                 'name' => 'Product Category'
  29.                         ),
  30.                         'rewrite' => array('slug' => 'product'), //这里维持原样
  31.                         'show_ui' => true,
  32.                         'show_tagcloud' => false,
  33.                         'hierarchical' => true
  34.                 )
  35.         );
  36. }
复制代码
最后,在post_type_link钩子上添加代码根据类型名称替换实际生成的url:
  1. add_filter('post_type_link', 'custom_type_link', 1, 3);
  2. function custom_type_link( $link, $post = 0 ){
  3.         if ( false !== strpos( $link, '%product_category%' ) ) {
  4.                 $taxonomy_terms = get_the_terms( $post->ID, 'product_category' );
  5.                 if( is_array($taxonomy_terms) ){
  6.                         foreach ( $taxonomy_terms as $term ) {
  7.                                 if ( ! $term->parent ) {
  8.                                         $link = str_replace( '%product_category%', $term->slug, $link );
  9.                                 }
  10.                         }
  11.                 }
  12.         }
  13.         return $link;
  14. }
复制代码
如此,两个url就能和平共处了。这个问题的修复逻辑是这样的:因在两处注册rewrite的时候使用同一个slug名,会造成rewrite冲突,产生404报错,那么我们就避免使用相同的slug,把第一处的slug改成’product/%product_category%’绕开这个问题。在实际生成url的时候再把类型名替换’%product_category%’字符串,从而解决问题。

道勤主机提供365天*24小时全年全天无休、实时在线、零等待的售后技术支持。竭力为您免费处理您在使用道勤主机过程中所遇到的一切问题! 如果您是道勤主机用户,那么您可以通过QQ【792472177】、售后QQ【59133755】、旺旺【诠释意念】、微信:q792472177免费电话、后台提交工单这些方式联系道勤主机客服! 如果您不是我们的客户也没问题,点击页面最右边的企业QQ在线咨询图标联系我们并购买后,我们为您免费进行无缝搬家服务,让您享受网站零访问延迟的迁移到道勤主机的服务!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

道勤网- 推荐内容!上一条 /2 下一条

!jz_fbzt! !jz_sgzt! !jz_xgzt! 快速回复 !jz_fhlb! !jz_lxwm! !jz_gfqqq!

关于我们|手机版|小黑屋|地图|【道勤网】-www.daoqin.net 软件视频自学教程|免费教程|自学电脑|3D教程|平面教程|影视动画教程|办公教程|机械设计教程|网站设计教程【道勤网】 ( 皖ICP备15000319号-1 )

GMT+8, 2024-5-6 02:23

Powered by DaoQin! X3.4 © 2016-2063 Dao Qin & 道勤科技

快速回复 返回顶部 返回列表