Reputation: 1262
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.com/john-doe" he have a post here and the url is, "www.domain.com/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
}
Upvotes: 1
Views: 4545
Reputation: 1262
I found the answer at 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;
}
Upvotes: 2