Conditional: Only if Category has Posts

Fundamental 543 views
0 Flares 0 Flares ×

If you only want to display something based on the fact of whether there are any posts in a particular category or not this piece of code is priceless.

Single Category

<?php if (get_category('17')->category_count > 0) { ?>

<?php } ?>

Use Case

<?php if (get_category('16')->category_count > 0) { ?>

	<?php query_posts('cat=16'); ?>
	<?php while (have_posts()) : the_post(); ?>

			<h2><?php the_title(); ?></h2>
			<?php the_content(); ?>
		<div class="clear"></div>

	<?php wp_reset_query(); ?>			

<?php } ?>

For Reference:

How to Display a Category Only if it has Posts
Conditional Tags

Tagged with:
Posted in Wordpress


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

No Trackbacks.


Register | Lost your password?
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×