在WordPress中如果注册一个自定义文章类型(Custom Post Type),并且同时为这个类型注册一个自定义分类法(Custom Taxonomy),两者使用同一个根slug,访问这个类型的页面就会发生404报错。
这个slug具体指什么呢?举例说明:
https://www.my-site.com/product/123.html
https://www.my-site.com/product/term-name
以上URL分别作为product类型页面的详情页和分类列表页,URL中的product就是根slug。在使用register_post_type注册product文章类型的时候,代码体现为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | add_action('init', 'create_product_post_type', 0); function create_product_post_type() { register_post_type('product', array( 'label' => 'Products', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'product'), //注意此行 'supports' => array('title','editor','thumbnail'), 'labels' => array ( 'name' => 'Products', 'singular_name' => 'Product', 'menu_name' => 'Products' ) ) ); } |
注意第11行的rewrite参数,就为此自定义类型指定了根slug,即形成如下url:
https://www.my-site.com/product/123.html
查看详细