WordPress.org

Ready to get started?Download WordPress

Forums

How to get parent category name? (4 posts)

  1. olddocks
    Member
    Posted 5 years ago #

    Lets say i am creating 3 parent categories and 10 sub categories to each parent. My wordpress post belongs to one sub category of a particular parent category

    How do i get the parent category name ONLY? i dont want subcategories names? what wordpress code would do that?

  2. spade5702
    Member
    Posted 4 years ago #

    I would like to know how the get the category parent name too. Can anyone please help?!!

  3. Aldi
    Member
    Posted 4 years ago #

    First, get the parent's ID then the name... Something like the one below, but you should adjust it to your needs. The example below is good if you only have one sub category.

    
    <?php
    foreach((get_the_category()) as $childcat) {
      $parentcat = $childcat->category_parent;
      echo get_cat_name($parentcat);
    }
    ?>
    
    
  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    If you wanted to get the parent category for the category currently being queried, you could use.

    <?php echo get_category_parents( get_query_var('cat') , true , '' ); ?>

    It should show each parent.. supports a seperator to.

    Set true to false to not display as a links..

    More info (though somewhat vague).
    http://codex.wordpress.org/Function_Reference/get_category_parents

Topic Closed

This topic has been closed to new replies.

About this Topic