Jump to content

Wordpress - get_categories - style empty categories differently


erikw46

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.