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

Pierre Smith
Pierre Smith
11,842 Points

How do I implement else w/ the if / while format given?

I this video we have to do this

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

to allow for easy text editing. How do I add an else to this?

2 Answers

Yojance Rabelo
Yojance Rabelo
1,156 Points
<?php  if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
Your else stuff
<?php endif; ?>

Do you need all those separate php tags? can you get away with :

<?php  
  if ( have_posts() ) : while ( have_posts() ) : the_post(); 
       the_content(); 
  endwhile; else:
       Your else stuff
  endif; 
?>

While it's probably too late to give a timely response, having the php in seperate blocks will allows you enter html seperately from the php content.

Otherwise you would either have html in php code which would error out, or you would have to echo it out each time.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
           <?php the_content(); ?>
    <!-- HTML could be here instead of the php call. 
           You might want to have the title specifcally in a <h2>
           and the_content() in a <p> -->
    <?php endwhile; else: ?>
         <!-- HTML could be here instead of the php call. 
               You might want to have the title specifcally in a <h2>
                and the_content() in a <p> -->
    <?php endif; ?>