Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Hide headings outside WP_Query if empty WordPress

  • SOLVED

I need to hide a heading outside of a wp_query if the wp_query is empty? (Empty meaning there are no posts associated with category 9)

<em>Please note</em> I use cat -16 as an archive fucntion, so if a post is under category 16 it does not display in the loop.

<strong>So I'm trying to get it working like this:</strong>

1. If WP_Query('cat=9,-16&showposts=99');
2. Detects that Category 9 has no posts
3. The heading <h3>The Code</h3> is not displayed




<h3>The Code</h3>
<?php $my_query = new WP_Query('cat=9,-16&showposts=99');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>

<article class="home-article clearfix">
<a href="<?php the_permalink() ?>">
<figure class="thumbnail">
<?php $meta_thumb_image = get_post_meta( get_the_ID(), 'meta_large', true ); ?>
<?php echo wp_get_attachment_image($meta_thumb_image, 'thumbnail');?>
</figure>
<div class="content">
<h4><?php the_title(); ?></h4>
<p><?php $meta_summary_text = get_post_meta( get_the_ID(), 'meta_summary_text', true ); ?><?php echo $meta_summary_text ?></p>
<p class="more">more</p>
</div>
</a>
</article>
<?php endwhile; ?>

Answers (3)

2012-08-21

Abdelhadi Touil answers:

Hi.
Try this:
<?php $my_query = new WP_Query('cat=9,-16&showposts=99');

if($my_query->have_posts()) { ?>
<h3>The Code</h3>
<?php while ($my_query->have_posts()) : $my_query->the_post();

$do_not_duplicate = $post->ID; ?>



<article class="home-article clearfix">

<a href="<?php the_permalink() ?>">

<figure class="thumbnail">

<?php $meta_thumb_image = get_post_meta( get_the_ID(), 'meta_large', true ); ?>

<?php echo wp_get_attachment_image($meta_thumb_image, 'thumbnail');?>

</figure>

<div class="content">

<h4><?php the_title(); ?></h4>

<p><?php $meta_summary_text = get_post_meta( get_the_ID(), 'meta_summary_text', true ); ?><?php echo $meta_summary_text ?></p>

<p class="more">more</p>

</div>

</a>

</article>

<?php endwhile; ?>
<?php } ?>

Hope it works.


Nick comments:

Awesome thats works!

2012-08-21

Maor Barazany answers:

Hi,
Try to replace

new WP_Query('cat=9,-16&showposts=99')

with

new WP_Query(array('category__in => array(9,-16), 'posts_per_page' => 99))

2012-08-21

Michael Caputo answers:

How about:


<?php $my_query = new WP_Query('cat=9,-16&showposts=99');

if($my_query != '') : ?>

<h3>The Code</h3>

<?php
while ($my_query->have_posts()) : $my_query->the_post();

$do_not_duplicate = $post->ID; ?>



<article class="home-article clearfix">

<a href="<?php the_permalink() ?>">

<figure class="thumbnail">

<?php $meta_thumb_image = get_post_meta( get_the_ID(), 'meta_large', true ); ?>

<?php echo wp_get_attachment_image($meta_thumb_image, 'thumbnail');?>

</figure>

<div class="content">

<h4><?php the_title(); ?></h4>

<p><?php $meta_summary_text = get_post_meta( get_the_ID(), 'meta_summary_text', true ); ?><?php echo $meta_summary_text ?></p>

<p class="more">more</p>

</div>

</a>

</article>

<?php endwhile; endif; ?>


Nick comments:

Hi Michael, thanks for this, unfortunately the heading '<h3>The Code</h3>' still appears, even though cat=9 is empty.


Michael Caputo comments:




<?php $my_query = new WP_Query('cat=9,-16&showposts=99'); ?>

<?php if(!in_category( '9' ) OR in_category('16')) : ?>
<h3>The Code</h3>
<?php endif; ?>

<?php while ($my_query->have_posts()) : $my_query->the_post();

$do_not_duplicate = $post->ID; ?>



<article class="home-article clearfix">

<a href="<?php the_permalink() ?>">

<figure class="thumbnail">

<?php $meta_thumb_image = get_post_meta( get_the_ID(), 'meta_large', true ); ?>

<?php echo wp_get_attachment_image($meta_thumb_image, 'thumbnail');?>

</figure>

<div class="content">

<h4><?php the_title(); ?></h4>

<p><?php $meta_summary_text = get_post_meta( get_the_ID(), 'meta_summary_text', true ); ?><?php echo $meta_summary_text ?></p>

<p class="more">more</p>

</div>

</a>

</article>

<?php endwhile; ?>