在WordPress中仅为自定义帖子类型的固定链接显示特定类别

Yen*_*enn 7 wordpress permalinks custom-post-type

我试图在Wordpress Devlopment网络中提出这个问题没有成功,我试图在固定链接类型的永久链接中只显示3个特定类别.

现在,永久链接结构(即我正在使用的主题给出的结构以及我通过子主题修改的结构)如下:

www.myfoodblog.com/recipes/the-post-title/
         ^            ^           ^    
       root   custom-post-type  title  
Run Code Online (Sandbox Code Playgroud)

一些帖子属于3个类别,我想在永久链接中显示restaurant,users并且admin得到类似的东西

www.myfoodblog.com/recipes/restaurant/the-post-title/

www.myfoodblog.com/recipes/users/the-post-title/

www.myfoodblog.com/recipes/admin/the-post-title/
Run Code Online (Sandbox Code Playgroud)

将原始结构留给不属于这些类别的帖子.

我尝试使用此插件,但它会显示所有帖子的自定义帖子类型类别.

我也尝试按照此问题中提供的说明进行操作,但它不起作用,永久链接结构中没有任何更改.

任何建议都非常感谢.

ste*_*teo 4

您必须同时使用两者post_type_link并添加一个rewrite rule

function recipes_post_link( $post_link, $id = 0 ){
    $post = get_post( $id );
    if ( is_object( $post ) ){
        $terms = get_the_terms( $post->ID, 'recipe-category' );
        foreach($terms as $term) {
            if( $term->slug == 'restaurants' || $term->slug == 'users'){
                return str_replace( site_url()."/recipes" , site_url()."/recipes/".$term->slug , $post_link );
            }
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );

function custom_rewrite_basic() {
  add_rewrite_rule('^recipes/(.+)/(.+)', 'index.php?recipe=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Run Code Online (Sandbox Code Playgroud)