WordPress.org

Ready to get started?Download WordPress

Forums

Getting JUST the top-level category names (parent) (3 posts)

  1. Chris Demetriad
    Member
    Posted 2 years ago #

    wp: latest
    theme: notorious

    In the backend, I have a section called portofolio, with few categories as follows: drinks, food, whatever.

    In each one of these I've got two or three more subcategories and the subcategories all have few posts.

    Ex:

    drinks
    -juices
    -etc
    -bla bla
    food
    -fish
    -meat
    -veggie

    Now, on the portfolio page I have this foreach to get the top level category names out but my function:

    $terms = get_terms('portfoliocategory', 'hide_empty=1&pad_counts=1&child_of=0'); $count = count($terms);

    ..gets out the names of each category and sub-category. I only need the top-level ones (without alterating the structure tree of these categories).

    What function should I use, what arguments, any examples/hints please? I think I have tried everything.

    Thank you.

    and part of my foreach:

    foreach ( $terms as $term ) {
    echo '<li class="'.$term->name.'">
    <a class="portfoliobutton_noselect" href="#">'.$term->name.'</a></li>';} ETC, ETC
  2. Digital Raindrops
    Member
    Posted 2 years ago #

    Have a look at the Codex

    parent
    (integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.

    &parent=0

    HTH

    David

  3. Chris Demetriad
    Member
    Posted 2 years ago #

    Thanks David, I've tried that argument aswell, doesn't work as it only gets the top level categories which have no subcategories at all.

    If there is a parent category without any sub-categories in it, whether empty or not, it will not display.

Topic Closed

This topic has been closed to new replies.

About this Topic