help with WP_Query code

Discussion in 'WordPress' started by sixrfan, May 14, 2013.

  1. #1
    on this page (http://www.goldcoastchamber.com) i have this code which pulls in the Upcoming Events category post titles:

    <h2>Gold Coast Chamber Upcoming Events</h2>
    Coming Soon...
    <?php
      $loop = new WP_Query( array(
        'category_name' => 'Events',
        'orderby' => 'meta_value',
        'meta_key' => 'eventdate',
        'order' => ASC)
      );
      while ( $loop->have_posts() ) : $loop->the_post(); ?>
      <div class="sing-event">
        <p class="up-events"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <div id="teaser"><img src="<?php $thumb = get_post_custom_values('teaser'); echo $thumb[0]; ?>" alt="<?php the_title(); ?>" /></div></div>
    <?php endwhile; ?>
    Code (markup):

    We don't have any posts from that category at the moment so I inserted the text "Coming Soon..." but i'm wondering how I could fit that Coming Soon... into the code so that it gets auto-generated when there's no posts in that category.
    any idea? please advise. thanks in advance!
     
    sixrfan, May 14, 2013 IP
  2. Devtard

    Devtard Notable Member

    Messages:
    850
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    220
    #2
    Try this:

    <h2>Gold Coast Chamber Upcoming Events</h2>
     
    <?php
      $loop = new WP_Query( array(
        'category_name' => 'Events',
        'orderby' => 'meta_value',
        'meta_key' => 'eventdate',
        'order' => ASC)
      );
     
    if($loop->have_posts()){ //simple condition checking if the query has any results
      while ( $loop->have_posts() ) : $loop->the_post(); ?>
      <div class="sing-event">
        <p class="up-events"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <div id="teaser"><img src="<?php $thumb = get_post_custom_values('teaser'); echo $thumb[0]; ?>" alt="<?php the_title(); ?>" /></div></div>
    <?php endwhile;
    }
     
    else{
    echo 'Coming Soon...'; //this will be displayed if no results are available
    }
    ?>
    Code (markup):
     
    Devtard, May 14, 2013 IP
  3. sixrfan

    sixrfan Well-Known Member

    Messages:
    354
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    bingo. thanks!
     
    sixrfan, May 14, 2013 IP