最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

terms - Display custom taxonomy on single post

programmeradmin3浏览0评论

I have 4 different custom entries from my custom category.

Categories are: john-doe, jane-doe, john-does, jane-does

Each of the user have different posts. Let's say John Doe have a post, but on that post, I wanted to display his name via the post, but how can I fetch his category name "John Doe" without using the tax_query?

For example: John Doe have a post (same with his custom category name) and the url is, "www.domain/john-doe" he have a post here and the url is, "www.domain/portfolio/name-of-the-post. This post have all the contents except the category. So by that, How would I know if the post custom category was from John Doe without declaring or using the code below?

I tried get_the_category(), get_category() but throws error.

PS. I use this code and it was working fine to display all posts under their names manually.

$my_query = new WP_Query( array(
    'post_type'=>'news',
    'posts_per_page'=>4,
    'tax_query'=>array(
        array(
            'taxonomy'=>'portfolio_users',
            'field'=>'slug',
            'terms'=>'john-doe'  // change to real slug
        )
     )
) );

while ( $my_query->have_posts() ) {
    $my_query->the_post();
    // display post
}

I have 4 different custom entries from my custom category.

Categories are: john-doe, jane-doe, john-does, jane-does

Each of the user have different posts. Let's say John Doe have a post, but on that post, I wanted to display his name via the post, but how can I fetch his category name "John Doe" without using the tax_query?

For example: John Doe have a post (same with his custom category name) and the url is, "www.domain/john-doe" he have a post here and the url is, "www.domain/portfolio/name-of-the-post. This post have all the contents except the category. So by that, How would I know if the post custom category was from John Doe without declaring or using the code below?

I tried get_the_category(), get_category() but throws error.

PS. I use this code and it was working fine to display all posts under their names manually.

$my_query = new WP_Query( array(
    'post_type'=>'news',
    'posts_per_page'=>4,
    'tax_query'=>array(
        array(
            'taxonomy'=>'portfolio_users',
            'field'=>'slug',
            'terms'=>'john-doe'  // change to real slug
        )
     )
) );

while ( $my_query->have_posts() ) {
    $my_query->the_post();
    // display post
}
Share Improve this question asked Dec 3, 2017 at 17:12 iMarkDesignsiMarkDesigns 2032 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I found the answer CSS Tricks and it works.

Here's the code I use.

$terms = get_the_terms( $post->ID , 'portfolio_user' );
foreach ( $terms as $term ) {
  echo $term->slug;
}
发布评论

评论列表(0)

  1. 暂无评论