Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a static front page

Ben Attenborough
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 Points

Providing fallback text in WordPress

Hi, I'm trying to create some fallback text so that if the user hasn't entered any text for a post a message appears instead of the text being blank. Here is my attempt at the code:

     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

     <?php the_content(); ?>

     <?php endwhile; ?>

        <?php else : ?>

     <p>Fallback copy</p>

    <?php endif; ?>  

It successfully shows the text if a post exists, but nothing shows if the post is blank (I want the Fallback copy text to display in that case). What am I doing wrong?

Andres Altuve
Andres Altuve
16,274 Points
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

     <?php the_content(); ?>

     <?php endwhile; ?>

        <?php else : ?>

     <p><?php echo ('Your fallback message'); ?></p>

    <?php endif; ?>  
Ben Attenborough
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 Points

Ah, I got it have_posts() just checks if the post exists, so I need to use $post->post_content !="" to check if the post actually has content. I'm not sure if I should only check if the content is empty after I have checked if the post exists? Thoughts? New code:

     <?php if ( have_posts() & $post->post_content != "" ) : while ( have_posts() ) : the_post(); ?>

     <?php the_content(); ?>

     <?php endwhile; else: ?>

     <p>Fallback copy</p>

    <?php endif; ?>