• Resolved Rhand

    (@rhand)


    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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Rhand

    (@rhand)

    <?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..

    Thread Starter Rhand

    (@rhand)

    <?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..

    Thread Starter Rhand

    (@rhand)

    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?

    Thread Starter Rhand

    (@rhand)

    Maybe

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

    could work. Just needs a little more work..

    Thread Starter Rhand

    (@rhand)

    <?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..

    Thread Starter Rhand

    (@rhand)

    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;
    
               }
    
           }
           ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get sub category’ is closed to new replies.