WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Limitate get_the_category to specific category ID ? (6 posts)

  1. humbucker
    Member
    Posted 4 years ago #

    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!

  2. keesiemeijer
    moderator
    Posted 4 years ago #

  3. keesiemeijer
    moderator
    Posted 4 years ago #

    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 . ' ';
    }
    }
    ?>
  4. humbucker
    Member
    Posted 4 years ago #

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

  5. keesiemeijer
    moderator
    Posted 4 years ago #

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

  6. humbucker
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags