Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End ul Problems in PHP

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #44535
    siouxfan45
    Participant

    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](http://themeforward.com/demo2/ “”) 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](http://snippi.com/s/ibw6kgf “”).

    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.

    $categories = get_the_category();
    echo ‘

      ‘;
      foreach($categories as $category){
      $parent = $category->parent;
      if($category->parent == 0){
      }
      else{
      wp_list_categories(“child_of=$parent&title_li”);

      }
      echo ‘

    ‘;
    }
    ?>

    #133993
    __
    Participant

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

    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.

#134003
__
Participant

…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.

#134009

Unless I’m mistaken, just change this:

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

    to

    $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.

    #134014
    __
    Participant

    [found it](http://codex.wordpress.org/Template_Tags/wp_list_categories).

    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:

    $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>*

    #134019
    CrocoDillon
    Participant

    @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

    #134027
    __
    Participant

    > @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.

    #134036
    CrocoDillon
    Participant

    Ah, I missed that!

    Viewing 8 posts - 1 through 8 (of 8 total)
    • The forum ‘Back End’ is closed to new topics and replies.