• Hiya,

    I am (trying) to understand new WP_Query but am not sure of the correct syntax for an if… else statement.

    Can anyone give me starter on this?

    I have this currently:

    <?php
    	$my_query = new WP_Query('category_name=review&post_status=future&order=ASC');
    	while ($my_query->have_posts()) : $my_query->the_post();
    	$do_not_duplicate = $post->ID;
    	?>
    
    ** STUFF HERE ***
    
    	<?php endwhile;?>

    But how do I incorporate an if… else if there is no content to show?

    Hope you can help 🙂

    Britney

Viewing 4 replies - 1 through 4 (of 4 total)
  • Have you tried this out on your site? I think that if there’s no content to show, it won’t display anything at all. I suppose you could try this instead:

    <?php
    	$my_query = new WP_Query('category_name=review&post_status=future&order=ASC');
    // If there's content -- there's an IF statement added here
    	if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
    	$do_not_duplicate = $post->ID;
    	?>
    
    ** STUFF HERE ***
    
    	<?php endwhile;
    // If there's no content -- here's the ELSE statement
    else: ?>
    
    <!-- Do stuff -->
    <?php endif; ?>

    I haven’t tested that, so let me know how it works.

    Thread Starter britneyjackson

    (@britneyjackson)

    Hi tsguitar,

    Many, many thanks.

    That worked a treat although I am not sure if I have put the opening <ul> in the right place. If I put it in the query it will loop for every post. This is what I am using…

    <ul>
    	<?php
    	$my_query = new WP_Query('category_name=review&post_status=future&order=ASC');
    	// If there's content -- there's an IF statement added here
    	if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
    	$do_not_duplicate = $post->ID;
    	?>
    
    	<li><?php the_time('l, M j, Y') ?>: <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    	</ul>
    
    	<?php endwhile;
    
    	// If there's no content -- here's the ELSE statement
    	else: ?>
    
    	No gigs right now.
    
    	<?php endif; ?>

    Again thankyou for your help.

    Kind regards

    Put the closing UL after the whole thing:

    <?php endif; ?>
    </ul>

    And put “No gigs right now” inside an LI tag. That should do it.

    Thread Starter britneyjackson

    (@britneyjackson)

    Great! 🙂

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘new WP_Query question’ is closed to new replies.