1

This code displays a list of child categories of the parent category while a user is on a child category or single post.

The styling around this list is displaying even when a list isn't being generated. Take a look here where you'll see an empty black box - I've colored it black simply to make it stick out. Not relevant, but this code is impacted by another code in my functions.php.

How do I correctly insert the tags into this code and prevent a list from being generated when no child categories exist?

<?php
  $categories = get_the_category();
      echo '<ul style="background:#000">';
  foreach($categories as $category){
      $parent = $category->parent;
      if($category->parent == 0){
      }
      else{
          wp_list_categories("child_of=$parent&title_li");

      }
      echo '';
  }
  ?>
2

Check that you actually have categories before creating the list, and move the lines that echo the <ul> inside the conditional.

$categories = get_the_category();
if (!empty($categories)) {
  foreach($categories as $category){
    $parent = $category->parent;
    if($parent != 0){
      echo '<ul style="background:#000">';
        wp_list_categories("child_of={$parent}&title_li");
      echo '</ul>';
    }
  }
}

This will generate multiple lists, not just one.

Here is a version that echoes only a single <ul>.

$categories = get_the_category();
$catli = '';
if (!empty($categories)) {
  foreach($categories as $category){
    $parent = $category->parent;
    if($category->parent != 0){
      $catli .= wp_list_categories("child_of=$parent&title_li&echo=0");
    }
  }
  if (!empty($catli)) {
    echo '<ul style="background:#000">'.$catli.'</ul>';
  }
}
  • For some reason this is still returning the black <ul> box, any ideas? themeforward.com/demo2 – AndrettiMilas May 4 '13 at 15:16
  • The new code doesn't have the functionality of the original - it doesn't work. – AndrettiMilas May 4 '13 at 16:59
  • @AndrettiMilas : That code creates multiple <ul>s, which was noted in the post, but the categories returned are exactly the same as those from your original code, as near as I can tell. I added a version that only generates a single <ul>. It would help if you were to explain exactly what functionality is missing. – s_ha_dum May 4 '13 at 17:29
  • The code in my original question outputs child categories for the parent category only, these categories are displayed on both the categories page and the single post. The code you provided outputs the parent categories AND child categories. It also outputs it too many times when in a child category. It just isn't working. – AndrettiMilas May 4 '13 at 17:48
  • 1
    I was checking for the ID too strictly-- I had an extra =. I pasted your code immediately above mine in and the output is exactly the same for single posts pages and archive pages. – s_ha_dum May 4 '13 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.