Looking to create a dynamic blog section on your website using WordPress? This tutorial will walk you through the process of adding and customizing posts, with a focus on implementing the WordPress Loop function to access and display your blog's content.
Key Insights
- The WordPress Loop is a series of functions that allows access to a page's content, cycling through all available posts to display their titles, content, tags, comment links and more.
- The process of adding the Loop to your site involves manipulating the code within the 'primary' div of your site's 'index.php' file.
- Further code adjustments allow for the display of each post's content and title, enhancing the dynamic nature of your blog section.
- Creating a new post to test the Loop function can help confirm that WordPress is correctly displaying all of the posts on your site.
- To improve navigability, post titles can be made clickable, redirecting visitors to a dedicated page for each post.
- This process involves using the 'the_permalink()' function, which retrieves the appropriate link for each post.
Learn how to leverage WordPress features for building a dynamic blog section of a website, including displaying posts, adding and customizing posts, and implementing the Loop—a series of WordPress functions that facilitate access to a page's content.
This exercise is excerpted from Noble Desktop’s past WordPress training materials and is compatible with WordPress updates through 2020. To learn current skills in WordPress, check out our WordPress Bootcamp and coding bootcamps in NYC and live online.
Topics Covered in This WordPress Tutorial:
Displaying Posts, Adding/customizing Posts
Exercise Preview
Exercise Overview
In this exercise, we will begin building the blog section of the site. It’s typical for blogs to display the last 5–10 posts on one page. The content of a page is accessed through a series of WordPress functions commonly referred to as the Loop. It’s called the Loop because it loops through all of the available posts to pull out all their titles, content, tags, comment links, etc. This is the backbone of WordPress content.
If You Did Not Do the Previous Exercise
- In a web browser, go to localhost:8888/mrp/wp-admin (Mac) or localhost/mrp/wp-admin (Windows) and log in if prompted.
- On the left of the Dashboard, mouse over Appearance and click Themes.
- Click the Add New button.
- Click the Upload link, then the Browse (or Choose File) button.
- Navigate to Desktop > Class Files > WordPress.org Class and double–click mrpTheme-ready-for-blog.zip to open it.
- Click Install Now, then click Activate.
Adding the Loop
In a code editor, open index.php from the mrpTheme folder if it isn’t already open.
Around lines 28–57, delete everything inside the #primary div.
-
You should be left with an empty div:
<div id="primary"> </div><!—end primary—>
-
Inside the #primary div, add the following:
<div id="primary"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php endwhile; ?> <?php endif; ?> </div><!—end primary—>
Essentially, this says if there are posts, get the posts until there aren’t any left.
-
We still need to add code between these tags to display the content for each post. Add the following:
<div id="primary"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php the_content(); ?> <?php endwhile; ?> <?php endif; ?> </div><!—end primary—>
Save the file.
- Open up a browser and go to:
- Mac: localhost:8888/mrp
- Windows: localhost/mrp
You should see the default post that comes with the base install. Yahooie! That means the site’s content is now dynamic!
- Let’s add another post to make sure WordPress is looping through and displaying all the posts. In the browser, go to:
- Mac: localhost:8888/mrp/wp-admin
- Windows: localhost/mrp/wp-admin
If asked, enter your username and password.
On the left, mouse over Posts and click Add New.
For the title, enter: New Post
For the content, enter: This is the new post content.
On the right, click Publish.
Click the Monteith Restoration & Performance link at the top to preview the site.
The new post should be visible, but lacks the title and blends in with the other one. Let’s add the title into the code. Switch back to index.php in your code editor.
-
Above the_content
()
function (around line 29), add the following:<div id="primary"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <?php endwhile; ?> <?php endif; ?> </div><!—end primary—>
NOTE: This function displays the title of the current post. Remember that even though it’s only here once in the code, this section will be looped over for each post.
Save the file.
- Return to the browser and reload:
- Mac: localhost:8888/mrp
- Windows: localhost/mrp
Click the title of a post. It won’t do anything, yet. Typically on blogs, clicking the post title brings you to its own page (where you can leave comments and such).
Switch back to your code editor.
-
Wrap the title in the following link:
<div id="primary"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_content(); ?> <?php endwhile; ?> <?php endif; ?> </div><!—end primary—>
NOTE: the_permalink
()
gets the appropriate link for the post. Save the file.
- Return to the browser and reload:
- Mac: localhost:8888/mrp
- Windows: localhost/mrp
Click the title of one of the posts. It should bring you to a page that shows just that post. Success!