how to display specific Category if post have more one Category?
in single.php, i want to get specific Category for post
i need a code that cheks all post Category and display just the one in specific main Category.
like that
$category = get_the_category();
if category( in_parent('11') ){
$parent = $category[1]->category_parent;
}
for my site maktaba
how to display specific Category if post have more one Category?
in single.php, i want to get specific Category for post
i need a code that cheks all post Category and display just the one in specific main Category.
like that
$category = get_the_category();
if category( in_parent('11') ){
$parent = $category[1]->category_parent;
}
for my site maktaba
Share Improve this question edited Sep 24, 2019 at 14:34 Ali asked Sep 24, 2019 at 14:28 AliAli 32 bronze badges1 Answer
Reset to default 0Confusingly, get_the_category()
returns an array of categories, so you're going to need to loop through them.
It sounds like you want whichever category is assigned to the current post, and is also a sub-category of category id 11. If that's the case, use
<?php
// Get all the categories assigned to this post
$categories = get_the_category();
// Loop through the array that was returned
foreach($categories as $category) {
// If this category is a sub-category of category 11
if($category->parent == 11) {
// Set the $parent to it
$parent = $category;
// And exit the foreach loop
break;
}
}
?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745132709a4613049.html
评论列表(0条)