WordPress.org

Ready to get started?Download WordPress

Forums

Conditional statement if no posts to display (3 posts)

  1. matreece
    Member
    Posted 1 year ago #

    Hello, I'm am trying to add some code in that if there are no posts to display then a line of text like 'Sorry no posts to display' appears.

    I'm not very good on the php side of things so any help much appreciated.

    My code is -

    <div id="mainContent">
    <div id="homeNews">
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <div id="saleContainer">
    <div id="salePic"><?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail();
    }
     ?></div>
    
    <div id="saleContent">
    <h3><?php the_title(); ?></h3>
    <div class="date"><?php the_time('F jS, Y') ?></div>
    <?php the_excerpt(); ?> 
    
    </div>
    </div>
    <?php endwhile; ?>
    <?php endif; ?>
    </div>
    </div>
  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    Try;

    <div id="mainContent">
    <div id="homeNews">
    
    <?php if (have_posts()) { ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <div id="saleContainer">
    <div id="salePic"><?php if ( has_post_thumbnail() ) {
    	the_post_thumbnail();
    }
     ?></div>
    
    <div id="saleContent">
    <h3><?php the_title(); ?></h3>
    <div class="date"><?php the_time('F jS, Y') ?></div>
    <?php the_excerpt(); ?> 
    
    </div>
    </div>
    <?php endwhile; ?>
    <?php }
     else { ?>
    <h3>Sorry no posts to display</h3>
    <?php } ?>
    </div>
    </div>
  3. matreece
    Member
    Posted 1 year ago #

    That is fantastic - many thanks for your help. Worked a treat.

    Matt

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags