• <?php echo(get_category_parents($cat, TRUE, ‘ » ‘)); ?>

    will output:

    Internet ? Blogging ? WordPress ?

    How can I make it display the Parent Category Name only?

    Internet

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter elphi

    (@elphi)

    That’s where i found the code posted above. It shows the Parent Name and the child categories. Is there in any I can show only the parent name?

    function get_category_parent($id, $link = FALSE, $nicename = FALSE){
    	$chain = '';
    	$parent = &get_category($id);
    	if ( is_wp_error( $parent ) )
    		return $parent;
    
    	if ( $nicename )
    		$name = $parent->slug;
    	else
    		$name = $parent->cat_name;
    
    	if ( $link )
    		$chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>';
    	else
    		$chain .= $name;
    	return $chain;
    }

    Put that in your functions.php and call it with:

    <?php echo(get_category_parent($cat ,TRUE, TRUE)); ?>

    Thread Starter elphi

    (@elphi)

    thanks! i’ve tried it but it didn’t work. i change $cat to $category because it would be place after this code:

    $display_categories = array(22,15,25,26,29);
    foreach ($display_categories as $category) { ?>

    <?php echo(get_category_parent($category ,TRUE, TRUE)); ?>

    it displays the current SLUG not the parent category.

    This shows the parent category for each of the post’s categories. It also works outside the loop.

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

    corinutz, thx, it really works.

    Another question…

    <?php echo(get_category_parents($cat, TRUE, ‘ ? ‘)); ?>

    will output:

    Internet ? Blogging ? WordPress ? Test Category ?

    How can I make it display the Parent Category Name only?

    Blogging ? WordPress ? Test Category ?

    The “Blogging” category have id=32.
    How to show categories from current (child child of 32) to category with id 32 and trim alll other parent categories?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Show Category Parent name’ is closed to new replies.