How to Build a Custom Page Template for Blog Posts
When building out a custom “Blog” section for your WordPress site, sometimes it’s easiest to create a custom page template that you can customize that allows you to set the page location (such as http://upthemes.com/blog), as well as your very own title and description that are displayed at the top of the blog section.
Step 1. Copy index.php to tpl_blog.php
To start, copy your index.php file from your current theme and rename the copied file tpl_blog.php. The reason for this is to get an exact copy of the theme functions and HTML structure from your current theme and to create a page template that will eventually be selectable from the ‘Page Template’ dropdown.
Step 2. Create Page Template ID
In your new file tpl_blog.php, you need to add a special comment to the top of the file that WordPress will recognize as a custom template.
/* Template Name: Blog Posts */
Simply add that to the very top of your page and now WordPress will recognize it in the Edit Page screen sidebar.
3. Use Custom Query for Posts
Place this below the get_header() template tag:
This will override the current page query with a custom query for 10 paginated blog posts. Since we’re using
query_posts(), we don’t need to generate a new custom
WP_Query to store the custom query.
4. Add Navigational Links for Pagination
Below your post loop, add some links for pagination like so:
This should be between the
endif; sections of code in your theme.
5. Reset the Query
Once you’ve output all your posts and navigation, you can use
wp_reset_query() to reset the
query_posts() results that you created earlier. In lamens terms, you’ve now reset the query to what it was before you called
6. Putting it all together
Here’s a completed page template that utilizes everything described above with additional markup and includes the loop:
WordPress Tips & Tricks Newsletter
Sign up for our 'once-in-a-while' WordPress tips and tricks newsletter for all skill levels. We provide excellent tips for marketing, SEO, and customization for your WordPress site.