WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] show parent category (3 posts)

  1. JimVer
    Member
    Posted 7 years ago #

    I am trying to display only the top level parent category name. I tried get_category_parents but it shows the children too. I just want the parent name. I tried to modify get_category_parents with the code below and it is close but not quite right. It shows the correct name at the parent level but for the children it returns the parent ID and not the name. Can anyone help fix this? Thanks.

    function get_category_parent($id) {
    $parent = &get_category($id);
    $name = $parent->cat_name;

    if ( $parent->category_parent ) {
    $name = $parent->category_parent;
    }
    return $name;
    }

  2. Kafkaesqui

    Posted 7 years ago #

    "It shows the correct name at the parent level but for the children it returns the parent ID and not the name."

    That's because $parent->category_parent holds the ID for the parent. Try this mod to your if statement:

    if ( $parent->category_parent ) {
    $parent = &get_category($parent->category_parent);
    $name = $parent->cat_name;
    }

  3. JimVer
    Member
    Posted 7 years ago #

    Perfect, thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags