I apologize for my English, but I am French. In my custom search page I display the results of my custom post type and everything is good. Only, I would like to display above the custom post type the category that is his. To display the post categories I do:
$category = get_the_category ();
echo "<p> Category:". $category[0]->cat_name. "</ p>";
But how to do for custom post type?
Thank you for your help.
I apologize for my English, but I am French. In my custom search page I display the results of my custom post type and everything is good. Only, I would like to display above the custom post type the category that is his. To display the post categories I do:
$category = get_the_category ();
echo "<p> Category:". $category[0]->cat_name. "</ p>";
But how to do for custom post type?
Thank you for your help.
- 1 Welcome to WPSE. Do you have a custom taxonomy created for your post type? – jdm2112 Commented Apr 19, 2019 at 15:40
- yes, I have several – barale61 Commented Apr 19, 2019 at 17:14
1 Answer
Reset to default 0For a custom post type, use get_the_terms()
instead:
<?php $post_terms = get_the_terms( get_the_ID(), 'your-taxonomy' ); ?>
<p> Category: <?php echo $post_terms[0]->name; ?></p>
Much like the results from get_the_category
, the returned value of get_the_terms()
is an array of term objects for the taxonomy that you specify when calling the function. In the example code, that is "your-taxonomy".
This sample code will only display the first term if multiple terms are assigned to a post. That is the 'zero position' in the array. Any subsequent terms would be returned in $post_terms[1]
, $post_terms[2]
, etc
https://developer.wordpress/reference/functions/get_the_terms/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745567392a4633477.html
评论列表(0条)