Working with The Loop in WordPress Twenty Fifteen
The Loop is no different from any other WordPress template tag; it must begin with a function and it must end with a function. The Loop begins and makes the following request: While there are posts in my blog, display them on this page. This PHP function tells WordPress to grab the post information from the database and return it to the site.
The big if
PHP functions in a simple, logical manner by doing what you do on a daily basis — making decisions based on questions and answers. PHP deals with three basic variables:
The basic idea is this: IF this, THEN that, or ELSE this.
If, then, and else
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 WordPress), 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 in plain English reads: “If [this blog] does not have posts.”
In the Twenty Fifteen theme, if 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 search box 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 can’t find what you’re 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:
<div section class=““>
<div header class=““>
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.