Conditional: Only if Category has Posts

Fundamental 368 views
+1
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(); ?>

		<article>
			<h2><?php the_title(); ?></h2>
			<?php the_content(); ?>
		</article>
		<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

Discuss:

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.

Login

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