How to Use the if Statement in the Twenty Fifteen WordPress Main Index Template

By Lisa Sabin-Wilson

PHP functions in a pretty simple, logical manner. It functions by doing what you do on a daily basis — making decisions based on questions and answers. PHP deals with three basic variables:

  • if

  • then

  • else

The basic idea is this: IF this, THEN that, or ELSE this.

People deal with if, then, else situations every day, as in these examples:

  • IF I have a dollar, THEN I’ll buy coffee, or ELSE I won’t drink coffee.

  • IF it’s warm outside, THEN I’ll take a walk, or ELSE I won’t take a walk.

  • IF I understand this code, THEN I’ll be happy, or ELSE I won’t be happy.

If your blog has posts (and most do, even when you first install it), WordPress proceeds with The Loop, starting with the piece of code that looks like this:

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

This code tells WordPress to grab the posts from the MySQL database and display them on your blog page. Then The Loop closes with this tag:

<?php endwhile; ?>

In some themes, near the beginning of The Loop template you may see a template tag that looks like this:

<?php if ( ! have_posts() ) : ?>

The exclamation point (!) preceding the function: have_posts() means “does not.” So a translation of that template tag into plain English reads: If [this blog] does not have posts.

In the Twenty Fifteen theme, your blog meets that condition (that is, if it doesn’t have any posts), WordPress skips The Loop and displays a message that no posts exist by calling in the content-none.php template part:

<section class=“no-results not-found”>
<header class=“page-header”>
<h1 class=“page-title”><?php _e( ‘Nothing Found’, ‘twentyfifteen’ ); ?></h1>
</header><!-- .page-header →
<div class=“page-content”>
<?php if ( is_home() && current_user_can( ‘publish_posts’ ) ) : ?>
<p><?php printf( __( ‘Ready to publish your first post? <a href=“%1$s”>Get started here</a>.’, ‘twentyfifteen’ ), esc_url( admin_url( ‘post-new.php’ ) ) ); ?></p>
<?php elseif ( is_search() ) : ?>
<p><?php _e( ‘Sorry, but nothing matched your search terms. Please try again with some different keywords.’, ‘twentyfifteen’ ); ?></p>
<?php get_search_form(); ?>
<?php else : ?>
<p><?php _e( ‘It seems we can’t find what you’re looking for. Perhaps searching can help.’, ‘twentyfifteen’ ); ?></p>
<?php get_search_form(); ?>
<?php endif; ?>
</div><!-- .page-content →
</section><!-- .no-results →

WordPress displays the title Nothing Found, followed by a text statement for the visitor, depending on some factors that are seen in the if or else statements in the code snippet above:

  • <?php if ( is_home() && current_user_can( publish_posts ) ) : ?>: This statement says that if you are viewing the home page and you are currently logged in as a user who has the capability to publish posts on the site, then display the following text: Ready to publish your first post? Get started here.

  • <?php elseif ( is_search() ) : ?>: This statement says that if the visitor is on a search page, then display the following statement: Sorry, but nothing matched your search terms, please try again with some different keywords. This statement is followed by the template tag to include a searchbox that allows users to search the site for more information: <?php get_search_form(); ?>.

  • <?php else : ?>: This statement says that if the visitor is visiting a page that does not meet any of the above two conditions, then display the following statement: It seems we cant find what youre looking for. Perhaps searching can help. The template tag to include the search form, as in the previous example, follows this statement.

Mixed within the template tags and functions, you find basic HTML markup. For example:

&lt;section class=““&gt;
and
&lt;header class=““&gt;

HTML and CSS drive the formatting and style of your overall theme, from a visual standpoint (fonts, colors, images, and so on).

WordPress uses other template files besides the Main Index, such as the Header, Sidebar, and Footer templates.