Support » Fixing WordPress » Conditional statement if no posts to display

  • 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>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    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>

    Thread Starter matreece

    (@matreece)

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

    Matt

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditional statement if no posts to display’ is closed to new replies.