Because I don’t have my content.php file set up to load content, if I were to add a few posts to my WordPress site like you might do for other templates, those posts wouldn’t display on my site.
WordPress has a feature that they call “the loop” that lets you iterate over records in a collection. What this basically means is that if you have a bunch of posts, you can display those posts one-by-one. You can even use a template in the body of a loop to make sure that each post gets formatted as it should. With my blog as it currently is, that means the title goes at the top, the content of my post goes in the middle, and the metadata of my post goes at the end.
I’m going to create a loop on my index.php page that loads posts from my blog according to the content.php template I’ve already created.
<?php get_header(); ?> <!-- Display each post until there are no more posts --> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php get_template_part('content'); ?> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts!'); ?></p> <?php endif; ?> <?php get_footer(); ?>
Since I’ve added three posts to my site to test, my content.php template should load three times. It won’t yet be dynamic at this point because I haven’t told it to pull any of the values from the posts it’s pulling — only to pull as many times as there are posts.
The function that loops through my posts does a few things. First, it checks to see if there are any posts. Then, it displays the first post it finds. After that, as long as there are more posts to display, it will continue to display them. Once there are no more posts, it stops trying to display them. If it turned out there were never any posts to display in the first place, it would display a message to my site’s visitors that lets them know there are no posts available for their selection.
Why is this important?
Primarily, it’s important because we keep bringing up the word “post”. My posts are an instance of the
wp_post class in WordPress. Every post I make on my site has the same properties available as the
wp_post class, although the values may be different. This means that I can use the Class Reference for WPPost that WordPress provides to figure out how to display information from my posts.
One of the many concepts I have learned through trying new things over and over again is that figuring how to get to the answer is as important as getting the answer. I’m sure it won’t be long until I have my post content showing up.
My content.php file looks like this currently:
<div class="blog-post"> <h2 class="blog-post-title">My Clever Title</h2> <p>This is the content of the post. Not very fancy, but it's loaded.</p> <p class="blog-post-metadata">Steve Carroll — 9:42pm on September 3, 2016</p> <hr> </div>
From this, I have decided that I want to load the title from my posts, as well as the content, author, and date/time it was created. I’m not sure why, but while in the loop instead of individual pages, the properties must be prefixed by “the” instead of “post”. I’ve updated my content.php to access these properties from my posts.
<div class="blog-post"> <h2 class="blog-post-title"><?php the_title(); ?></h2> <p><?php the_content(); ?></p> <p class="blog-post-metadata"><?php the_author(); ?> — <?php the_date(); ?></p> <hr> </div>
And there you have it!
At this point, I’ve got a basic homepage set up where all my posts will be loaded to the page with new posts positioned at the top. I’ll probably want to handle pagination or some lazy loading at some point in the future, but I don’t have enough written yet to really justify that. So it makes sense at this point to start working on getting everything to look a bit better.