Monday , 20 November 2017
Home / Programming / WordPress / Themes / Single Post and Page Templates – WordPress Theme Tutorial
Single Post and Page Templates
Single Post and Page Templates

Single Post and Page Templates – WordPress Theme Tutorial

This tutorial is a part of the WordPress Theme tutorial series. If you would like to see the whole series and the order in which you should follow them, then click here.

WordPress is pretty smart when it comes to knowing what pages to use. The only template file you need is index.php. While it works great for our home page, it doesn’t work so well for displaying categories, archives, pages, and single posts. You can actually create a template for those pages.

Before WordPress decides on what page template to use, it checks the link query. The link query is basically information inside the link that tells WordPress what kind of content it is. For example, if someone clicks on a blog post, then WordPress will know it’s a blog post and search for a template for blog posts. If it doesn’t find anything, it’ll use index.php by default.

So, let’s create a template for single blog posts. Open up your index.php file and then save it as single.php. This is the file name WordPress will search for when someone clicks on a single blog post. Open up this file and let’s change a few things.

Single Post Template

Let’s update the <aside></aside> tags inside the loop to this.

We added a new section for tags. The the_tags() function is a function provided by WordPress. This function will return a list of tags for our blog post. It has 3 parameters. The first parameter is what should be displayed before each tag. The second is what should be displayed after each tag. Lastly, what should be displayed when all tags are outputted. As you can see, we’re just adding some simple HTML to format this output.

Page Template

Let’s create a template for pages. Pages don’t need all that information posted. They usually just contain a title and some content to go with it. So, save your single.php file as page.php. Once again, WordPress will search for a file called page.php when a user clicks on a page and not a post.

Update the loop to this.

As you can see, we stripped a lot of the formatting out and kept the page simple. Nothing new here. We also update the echo message for when the user searches for a page that isn’t available.

Conclusion

WordPress is a very powerful and flexible CMS. In the next tutorial, we’ll discuss about how to create error pages.

About Jasko Koyn

Check Also

How to add JavaScript in your WordPress Themes

How to add JavaScript in your WordPress Themes

We have some javascript in our theme. So, let’s learn how to add JavaScript in …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link