WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] show parent category

[Resolved] show parent category

  • 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;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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;
    }

    Perfect, thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] show parent category’ is closed to new replies.