I. Using index.php as a blog template
This method is very simple. It is default functionality in WordPress, with no additional code. It is used on many websites.
You should know one thing: some themes doesn’t have code for blogs in their index.php file, for such themes this method doesn’t work. In that case I recommend you to look at the second method, you can also find it in this post.
Step 1. Creating a Blog Page
First, go to the Pages > Add new, after that fill the title input field, I suppose it will be «Blog», and save the page.
Step 2. Customizing Locations for Homepage and Blog Posts
Go to the Settings > Reading and set up a static page as the homepage and our blog page as a posts page.
Step 3. Add a Menu Item
This is an unnecessary step. If you have finished the second step, the blog will appears on the page URL, you just created. The blog page will also use
index.php file as the template.
Anyway, if your current theme supports menus, you can add this page to website menus. If you want to do this, go to Appearance > Menus, add blog page to any menu and save it.
II. Using Your Own Page Template for Blog Posts
This method allows you to create blog sections using your own page template. This way is good when you need to display posts with your own parameters. You can also create multiple blog sections with different query arguments.
So, let’s begin.
Step 1. Creating Page Template
I suppose you already know how to do this, if you don’t, then open your current theme folder and create a php file there. You can name it as your want, e.g.
blog-template.php. Into this file add the following code:
<?php /* * Template name: Blog section template */
Step 2. Creating a Page
This step is similar to the first step in the first method, the only difference is that you should specify a page template in the «Page Attributes» section.
Step 3. Paging Navigation
Of course, we need a pagination for our blog section. You probably use some sort of plugins or your own function for these purposes. If something goes wrong, I recommend you to use WP-PageNavi plugin. I made some tests – this plugin works fine with the code from this tutorial.
Step 4. The Code for Page Template
This is just an example without some of important template functions such as
get_footer(). But this template contains everything you need to test your blog page.
<?php /* * Template name: Blog section template */ $current_page = (get_query_var('paged')) ? get_query_var('paged') : 1; // get current page number $args = array( 'posts_per_page' => get_option('posts_per_page'), // the value from Settings > Reading by default 'paged' => $current_page // current page ); query_posts( $args ); $wp_query->is_archive = true; $wp_query->is_home = false; while(have_posts()): the_post(); ?> <h2><?php the_title() /* post title */ ?></h2> <p><?php the_content() /* post content */ ?></p> <?php endwhile; if( function_exists('wp_pagenavi') ) wp_pagenavi(); // WP-PageNavi function
Only the best of WordPress
Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.