在WordPress的自定义帖子类型的永久链接中仅显示特定类别 [英] Show only specific categories in permalinks for custom post type in WordPress

查看:92
本文介绍了在WordPress的自定义帖子类型的永久链接中仅显示特定类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Wordpress开发网络中问这个问题,但没有成功,我试图在永久链接中仅显示3种特定的类别,以用于自定义帖子类型类别.

I tried to ask this question in the Wordpress Devlopment network with no success, I'm trying to display only 3 specific categories in permalinks for custom-post-type categories.

现在,永久链接结构(即,并且我正在通过子主题进行修改),如下所示:

Right now the permalinks structure (that is the structure given by the theme i'm using and that i'm modifying through a child theme)is as follows:

www.myfoodblog.com/recipes/the-post-title/
         ^            ^           ^    
       root   custom-post-type  title  

某些帖子属于3类,我希望在restaurantusersadmin的永久链接中显示以获得类似的信息

some post are under 3 categories that i would like to display in permalinks which are restaurant, users and admin to get something like this

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

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

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

为那些类别以外的帖子保留原始结构.

leaving the original structure for posts that are not in those categories.

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

I tried using this plugin but it will display the custom post type category for all posts.

我还尝试遵循

I also tried to follow the instructions provided in this question but it's not working, no changes are made in the permalinks structure.

任何建议都值得赞赏.

推荐答案

您必须同时使用post_type_link和添加rewrite rule

You have to work with both post_type_link and add a 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');

这篇关于在WordPress的自定义帖子类型的永久链接中仅显示特定类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆