categories - how to display specific Category if post have more one Category?

how to display specific Category if post have more one Category?in single.php, i want to get specific Category for posti

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Confusingly, 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

相关推荐

  • categories - how to display specific Category if post have more one Category?

    how to display specific Category if post have more one Category?in single.php, i want to get specific Category for posti

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信