我快速浏览了类似的问题,但大多数似乎并没有真正回答我的问题。
我有一个名为“产品”的自定义帖子类型。里面有房间类型、家具类型、材料等分类。
该网站需要一个非常具体的 url 结构来遵守其 SEO 策略。
此刻,我得到了这样的信息:
/room-type/handles/bathroom
/room-type/handles/kitchen
/room-type/handles/modern
我本质上需要删除房间类型。我尝试在注册分类法时将“with_front”设置为 false,但没有成功。
下面是我当前的代码,非常感谢任何帮助。
register_taxonomy(
'room-type',
'products',
array(
'label' => __( 'Room Type' ),
'rewrite' => array(
'slug' => 'room-type',
'with_front' => true,
'hierarchical' => true,
),
'hierarchical' => true,
)
);
更新
所以我做了一些挖掘,找到了这个页面:https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule
...这是我最终得到的代码 - 目前看来是我想到的最接近的代码:
function sb_rewrites() {
add_rewrite_rule( 'handles/([^/]+)/?', 'index.php?room-type=$matches[1]', 'top' );
}
add_action( 'init', 'sb_rewrites', 10, 0 );
function strip_room_type( $link, $term, $taxonomy ){
if ( $taxonomy !== 'room-type' )
return $link;
return str_replace( 'room-type/', '', $link );
}
add_filter( 'term_link', 'strip_room_type', 10, 3 );
谢谢!
据我了解您的问题,我不确定是否有完美的方法来删除该分类法。 WordPress 需要知道您在 URL 中要求的子分类法,因此它需要某种标识符。但是,似乎可以解决类似问题的答案:
https://wordpress.stackexchange.com/questions/21076/remove-taxonomy-base-or-term-from-url
本质上,定义详细的重写规则来“强制”该术语。但这可能会导致问题,在此处接受的答案中有更好的解释:
https://wordpress.stackexchange.com/questions/42120/remove-slug-in-taxonomy-url