WordPress.org

Ready to get started?Download WordPress

Forums

new WP_Query question (5 posts)

  1. BritneyJackson
    Member
    Posted 6 years ago #

    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

  2. tsguitar
    Member
    Posted 6 years ago #

    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.

  3. BritneyJackson
    Member
    Posted 6 years ago #

    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

  4. tsguitar
    Member
    Posted 6 years ago #

    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.

  5. BritneyJackson
    Member
    Posted 6 years ago #

    Great! :)

    Thank you

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.