- This topic is empty.
-
AuthorPosts
-
May 3, 2013 at 12:45 pm #44535
siouxfan45
ParticipantThis 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 ‘‘;
}
?>May 3, 2013 at 2:53 pm #133993__
Participantdon’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.
May 3, 2013 at 4:03 pm #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.
May 3, 2013 at 4:58 pm #134009will_wallace85
MemberUnless 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.
May 3, 2013 at 5:50 pm #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>*May 3, 2013 at 7:00 pm #134019CrocoDillon
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 :PMay 3, 2013 at 9:42 pm #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.
May 4, 2013 at 5:12 am #134036CrocoDillon
ParticipantAh, I missed that!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.