• Resolved Clicknathan

    (@clicknathan)


    Does anyone know how one might check to see if a category has a parent category?

    Specifically I’m trying to create a breadcrumb that outputs something like:

    Home » Parent Category » Subcategory

    But I’d like to be able to replace “Parent Category” with “if Subcategory has Parent Category, echo Parent Category” type of thing.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I would really like to do the same if anyone knows how.

    sure…
    I do it like this for my categories:

    <div id="breadcrumbs">
    <a href="<?php echo get_bloginfo('url'); ?>" title="">Home</a>
    <?php
      $breadcrumbs = explode('|',get_category_parents($cat,true,'|'));
      array_pop($breadcrumbs);
      array_pop($breadcrumbs);
      if ($breadcrumbs) foreach ($breadcrumbs as $crumb) echo ' / '.$crumb;
    ?>
    </div>
    <h1><?php single_cat_title(); ?></h1>

    That takes the current category off the end, and returns true on the last line only if your category has parents (and/or grandparents, etc.)

    if you don’t want to remove the category you’re in now, just remove one of those array_pop lines. I remove the current category so I can put it into that h1 tag underneath. You can see it working on my blog.

    if you just wanted to check for the top-level parent, you would just need to look at the first element in the $breadcrumbs array.

    if ($breadcrumbs) echo $breadcrumbs[0];

    hope that helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘If Category has Parent Category’ is closed to new replies.