I have a wp_query set up to show events and I want to show the date if we have posts and if we do not I do not want to show the date.

Here is my code:


            $today = getdate();
            $args = array(
                'post_type' => 'post',
                'category__not_in' => '-9', // When live, will need to add this parameter to all of the loop in this feedLoop.php - Do not forget to include this parameter as this function is needed!!! Cat No will change when live site is up and running!!! 
                 'order' => 'ASC',
                'post_status' => array(
                'date_query' => array(
                        'year'  => $today['year'],
                        'month' => $today['mon'],
                        'day'   => $today['mday'],
            $the_query = new WP_Query( $args );


<h3><? echo date('l, F jS'); ?></h3> <!-- this is the date
 but I do not want it to echo out three time if 
there are three posts!! -->

<?php if ( have_posts() ) :?> 

        <? while ( $the_query->have_posts() ) :?>

        <? $the_query->the_post(); ?>

    <strong><em><?php the_time(); ?></em></strong> - <a href="<?php the_permalink(); ?>" class="strong"><?php the_title(); ?></a>

    <?php the_excerpt(); ?>

<?php endwhile; else: ?>

    <p>There are no posts or pages here</p>

<?php endif; ?>

<?php wp_reset_query(); ?>

How can I query the arguments to see if there are posts and if so echoing out the date and hiding if not. Please someone put me out of my misery as I can not figure this out but it should be easy right?!!

Thanks for any help that I gain in advance!



<?php if ( $the_query->have_posts() ) :?> 

   <?php echo '<h3>' . date('l, F jS') . '</h3>'; ?>

   <?php while ( $the_query->have_posts() ) : ?>
     // etc. 
  • Tried that and 1, it doesn't hide it when there are no posts and 2, it echos out with the loop but I only want it to display once! Thanks :) – Tired_Man Jan 18 '15 at 14:59
  • i think the above code doesn't echo out date multiple times as it is not in the loop and only outputs when there are posts. – bravokeyl Jan 18 '15 at 15:12
  • Apologies bravokey, I didn't use the h3 tag that you offered by my own but what you have done is made me realise that I needed to put $thequery in front of have posts - that is where I have gone wrong. Thank you for your assistance it has helped me greatly. I will mark your answer as the correct one. Cheers can happy Sunday :) – Tired_Man Jan 18 '15 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.