• Resolved spintoband

    (@spintoband)


    I’m having the hardest time figuring this out:

    I want to list the parent category of a grandchild without listing its grandfather. I’m using this code to list parents:

    <?php	$category = $wp_query->get_queried_object();
    				$parent = ($category->category_parent) ? $category->category_parent : $cat;
    				echo get_category_parents($parent, TRUE, ' &raquo; ', FALSE );
    			?>

    I really have no need for those rackquotes as I only want to list the most immediate parent, and not go further up the chain!

    I was also messing around with this code, but the output of the $parent variable is an unlinked number, rather than a linked nicename. echo $parent->category_nicename; isn’t working:

    <?php
    								$cat_object = $wp_query->get_queried_object();
    								$parentcat = ($cat_object->category_parent) ? $cat_object->category_parent : $cat;
    								echo $parentcat;
    								?>

    Thanks in advance for any help!

    -spinto

Viewing 1 replies (of 1 total)
  • Thread Starter spintoband

    (@spintoband)

    Here’s the solution/hack I ended up using:

    <?php
     $category = $wp_query->get_queried_object();
     $parent = ($category->category_parent) ? $category->category_parent : $cat;
     $parents = get_category_parents($parent, TRUE, '&nbsp;', FALSE);
     $end = '&nbsp';
     $pos_start = strpos($parents, $end) ;
     $pos_end = strpos($parents, $end, ($pos_start+1));
     if ($pos_end === false) {
     echo $parents;  }
     else {
     echo substr($parents, ($pos_start), ($pos_end));
      }  ?>
Viewing 1 replies (of 1 total)
  • The topic ‘list category parent without listing grandparent category.’ is closed to new replies.