erikw46 Posted July 14, 2010 Share Posted July 14, 2010 Hello everyone, I have run into a bit of a snag while developing a wordpress site. What I have a is a function that lists all the subcategories of a parent. Each of the categories is displayed as a list item and when clicked they go to that categories page. However some of the categories that have been created have no posts in them yet so we dont wan't users to go to a category page that has no posts. So we would like for each empty category to not have an anchor around it. Here is the code that I have been modifying, it is in the functions.php file my custom theme. I feel like it is close but I just cant seem to get it to work. function servicesList(){ $args = array( 'orderby' => 'id', 'order' => 'ASC', 'child_of' => 8, 'hide_empty' => 0 ); $categories = get_categories($args); foreach($categories as $category) { if( empty( $category ) ) echo '<li>' . $category->name.'</li> '; else echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';} } Any help would be greatly appreciated! Thanks, Erik Quote Link to comment https://forums.phpfreaks.com/topic/207778-wordpress-get_categories-style-empty-categories-differently/ Share on other sites More sharing options...
sebajom Posted July 15, 2010 Share Posted July 15, 2010 I don't think the $category variable will ever be empty, even if it doesn't have any posts. Try changing the line: if( empty( $category ) ) to: if ($category->count > 0) Quote Link to comment https://forums.phpfreaks.com/topic/207778-wordpress-get_categories-style-empty-categories-differently/#findComment-1086429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.