自定义帖子类型/分类法URL结构

时间:2016-09-29 15:43:39

标签: wordpress seo custom-post-type

我已经快速浏览了类似的问题,但大多数人似乎并没有真正回答我的问题。

我有一个名为Products的自定义帖子类型。在那里,我有分类,如房间类型,家具类型,材料等等。

该网站需要一个非常具体的网址结构来遵守它的SEO策略。

目前,我得到的是这样的东西:

/room-type/handles/bathroom
/room-type/handles/kitchen
/room-type/handles/modern

我需要删除房间类型。我试过设置" with_front"注册分类标准时为假,但没有成功。

以下是我目前的代码,非常感谢任何帮助。

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 );

谢谢!

1 个答案:

答案 0 :(得分:0)

据我了解你的问题,我不确定是否有一个完美的方法来删除那个分类标本。 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

相关问题