Get a free trial // Grow your CSS skills // Land your dream job

ul Problems in PHP

  • # May 3, 2013 at 12:45 pm

    This is the code I am using to display a list of child categories of the parent category while a user is on a child category or single post. Of course, this is in WordPress.

    Since I have styled my tags, the styling around this list is displaying even when a list isn’t being generated. For example, [take a look here]( “”) where you’ll see an empty black box. I’ve colored it black simply to make it stick out. Not sure if this is relevant, but this code is impacted by [another code in my functions.php]( “”).

    I’d simply like a solution to correctly setup the < u l > (sorry for the spaces, had to for display purposes) tag so that it doesn’t display if no list is being generated.

    < ?php
    $categories = get_the_category();
    echo ‘

      foreach($categories as $category){
      $parent = $category->parent;
      if($category->parent == 0){

      echo ‘


    # May 3, 2013 at 2:53 pm

    don’t use `echo`: instead, generate the list first.

    < ?php
    foreach( $cetegories as $category ){
    // add each list item to an array
    $li[] = ‘

  • create your list item here
  • ‘;
    // once you’re done, check if there are any

  • s or not
    if( ! empty( $li ) ){
    // if not empty, add
      tags and implode all
    • s into a single string
      $list = ‘
        $list .= implode( “n”,$li );
        $list .= ‘

      // then print
      echo $list;
      // if empty, do nothing.

# May 3, 2013 at 3:13 pm

I’m sorry, but I lack the knowledge to combine what you wrote and the original code :-/

# May 3, 2013 at 4:03 pm

…and I don’t use WP : )

what does the function `wp_list_categories` do? Can you show an example of what it outputs?

Basically, my point is that, when you use `echo`, you’ve committed yourself to making a list (because you’ve already started to *output* a list), so it’s impossible to abort if you discover there is nothing to put in the list.

The solution, therefore, is to 1) make your list, 2) check if it is empty, and *then* 3) decide if you’re going to `echo` it.

# May 3, 2013 at 4:24 pm


wp_list_categories outputs a list of links

# May 3, 2013 at 4:58 pm

Unless I’m mistaken, just change this:

$li[] = '
  • create your list item here
  • ';


    $li[] = wp_list_categories("child_of=$parent&title_li");

    Not super familiar with list categories, and it probably just spits out a string so you could drop the [] from after the $li, but I think that'll do it.

    # May 3, 2013 at 5:50 pm

    [found it](

    IIUC, with the args you’re using, a category with no items will output an empty string. Is this correct?

    If so, you can do:

    < ?php
    $categories = get_the_category();
    foreach($categories as $category){
    $parent = $category->parent;
    if($category->parent != 0){
    $list[] = wp_list_categories( “child_of=$parent&title_li” );
    // this removes empty items in $list
    $list = array_filter( $list );
    // check if filtered list has any contents
    if( ! empty( $list ) ){
    print ‘

      ‘.implode( “n”,$list ).’


    *<rant> This is one of the reasons I don’t like WP. It has all kinds of built-in functions deliberately designed to spew half-baked output everywhere… </rant>*

    # May 3, 2013 at 7:00 pm

    @siouxfan45, what is the use of this line? `$parent = $category->parent;`, you don’t seem to be using that variable.

    @traq, you still left that first echo there :P

    # May 3, 2013 at 9:42 pm

    > @traq, you still left that first echo there :P

    lol, cr*p, fixed it. thanks

    (Of course, this is all still assuming that, if `wp_list_categories` doesn’t *find* anything, it doesn’t *return* anything [printable] either. I don’t know that for sure.)

    > what is the use of this line? $parent = $category->parent;, you don’t seem to be using that variable.

    he’s using it in the arg he’s passing:

    `wp_list_categories( “`**`child_of=$parent`**`&title_li” )`

    it limits the subset the categories returned.

    # May 4, 2013 at 5:12 am

    Ah, I missed that!

    # May 4, 2013 at 4:24 pm

    Thanks everyone :-)

    Viewing 11 posts - 1 through 11 (of 11 total)

    You must be logged in to reply to this topic.

    There's a whole bunch of content on CSS-Tricks.

    Search for Stuff   •   Browse the Archives

    Get the Newsletter ... or get the RSS feed