Support » Fixing WordPress » Limitate get_the_category to specific category ID ?

  • Resolved humbucker

    (@humbucker)


    Hello,

    I would like to know if it’s somehow possible to add an argument to the get_the_category function to only display categories (child of a parent category) a post belongs to.

    E.g if my posts belongs to cat 1, 1>2, 3, 7
    I d’like to write get_the_category(catId=1) so that it will ouput me the category name 1>2

    Anything possibile ??

    Thanks a lot!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Or this will only show child categories (use within the loop)

    <?php
    foreach((get_the_category()) as $category) {
    if($category->parent != 0){
    echo $category->cat_name . ' ';
    }
    }
    ?>

    If I use this solution, even the categories my post doesnt belong to will be shown … no ?

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    No, it will only show child categories that are assigned to the post.

    This is what I’ve done :

    <?php foreach((get_the_category()) as $chaine)
    { if($chaine->parent == 27){ echo $chaine->cat_name . ' ';  }
    }
    ?>

    It works…slighty modified but working.

    Thank you, you put me on the right tracks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limitate get_the_category to specific category ID ?’ is closed to new replies.