WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get sub category (7 posts)

  1. Rhand
    Member
    Posted 3 years ago #

    I need to grab the sub category. How can I do that?

    NB

    <?php $cat = get_the_category();
    	$parentCatName = get_cat_name($cat[0]->parent);
    	echo $parentCatName; ?>

    gets me the wrong ancestor, but not the parent

  2. Rhand
    Member
    Posted 3 years ago #

    <?php
    	$category = get_the_category();
    	echo $category[0]->cat_name;
    	?>

    Does get me a sub category, but the sub category of the wrong parent category..

  3. Rhand
    Member
    Posted 3 years ago #

    <?php $cat = get_the_category(); $cat = $cat[0];
    	echo(get_category_parents($cat, TRUE, ' &raquo; ')); ?>

    Gets me two categories of which one is the wrong parent category and the other a sub category of that same parent..

  4. Rhand
    Member
    Posted 3 years ago #

    Well, got a little further:

    <?php $cat = get_the_category();
    		$parentCatName = get_cat_name($cat[2]->parent);
    		echo $parentCatName; ?>

    gets me the right parent category, but I need to display $cat[1] when on a sub category of that page. How to do that?

  5. Rhand
    Member
    Posted 3 years ago #

    Maybe

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

    could work. Just needs a little more work..

  6. Rhand
    Member
    Posted 3 years ago #

    <?php
    foreach (get_the_category() as $cat) {
     $parent = get_category($cat->category_parent);
     $parent_name = $parent->cat_name;
     $parent_url =  $parent->slug;
    }
    ?>
    <?php echo $parent_name; ?>

    Shows wrong parent again and does not show the sub category or category child. Maybe I should just get the category..

  7. Rhand
    Member
    Posted 3 years ago #

    Got it.

    <?php
          foreach((get_the_category( get_the_ID() )) as $category)
            {
               if(cat_is_ancestor_of(get_cat_ID('x') , $category->cat_ID) )
                {
                $category_link = get_category_link( $category->cat_ID );
                $category_name = get_cat_name( $category->cat_ID );
                $Finalcategory = $category;
                break;
    
               }
    
           }
           ?>

Topic Closed

This topic has been closed to new replies.

About this Topic