Using Your Own Page Template for Separate Blog Pages
Table of Contents
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_header()
or 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