Whether you are running a website that invites users to read latest news and updates across different verticals like politics, movies, sports, etc, or you own a website that advertises you as a service provider, it would give your website a reasonable push if you create a separate blog site on the same URL. You can use that blog to publish your views, or articles that are atypical of standard blogs. If you play to the gallery by writing opinionated articles around the different theme your website caters to, you are set to see a jump in the number of visitors to your site.
However, building a custom blog section on the website involves an arrangement that is easy to create using PHP. The experts would recommend you create a custom page template while the process of building a blog section is being carried out. What this does is that it lends you a capability to define a desired page location that looks like myownoyster/blog. What’s more? You can even choose the title of your choice and display a short description on the topmost part of the page. Here is how you do it:
Reproduce the contents of index.php in the tpl_blog.php file
The first step in the process is opening the index.php file of the theme you are currently working on and copying its content in the new file created by the name of tpl_blog.php. You carry out this copy-paste task in order to make sure that theme functions and HTML structure are not lost in the way. It maintains the consistency of the files and helps carry their functions forward. This will also enable you to build a page template which can be accessed from a dropdown menu.
Make Page Template Readable for WordPress
The traditional practice of adding comments to code is used everywhere for the functions to become more comprehensible. WordPress is no different.
/* New Template: My Blog */
Hence, for the new file you just created (tpl_blog.php), append a comment to the file that tells WordPress that you are creating a custom template.
Use Custom Query for Posts
Now paste the below code right below the location where the get_header() template tag is mentioned:
<?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
This performs the overriding function on the existing page query by replacing it with a query that calls for 10 numbered blog posts. This process saves considerable time as the query_posts() function ensures that WP_Query doesn’t have to be called when there is a need to save the custom query.
Add Navigational Links for Pagination
In your code, between the endwhile; and endif; functions, add this code for pagination:
<div> <span><?php previous_posts_link(__('« fresh','demo')) ?></span> <span><?php next_posts_link(__('previous »','demo')) ?></span> </div><!-- /.navigation -->
This should be between the endwhile; and endif; sections of code in your theme.
The Reset Process
Now that you have successfully implemented the afore-mentioned steps, next in the line is using wp_reset_query() in order to retune the results that emerged by the usage of query_posts(). This takes the current query back to its original state.
Following is how the full page template would look like after combining additional markup with preceding procedures:
<?php get_header(); ?> <div id="content"> <?php query_posts('post_type=post&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?> <?php if( have_posts() ): ?> <?php while( have_posts() ): the_post(); ?> <div id="post-<?php get_the_ID(); ?>" <?php post_class(); ?>> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(200,220) ); ?></a> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <span><strong><?php the_time('F jS, Y'); ?></strong> / <strong><?php the_author_link(); ?></strong> / <span><?php comments_popup_link(__('0 comments','demo'),__('1 comment','demo'),__('% comments','demo')); ?></span></span> <?php the_excerpt(__('Continue reading »','demo')); ?> </div><!-- /#post-<?php get_the_ID(); ?> --> <?php endwhile; ?> <div> <span><?php previous_posts_link(__('« fresh','demo')) ?></span> <span><?php next_posts_link(__('previous »','demo')) ?></span> </div><!-- /.navigation --> <?php else: ?> <div id="post-404"> <p><?php _e('None found.','demo'); ?></p> </div><!-- /#post-404 --> <?php endif; wp_reset_query(); ?> </div><!-- /#content --> <?php get_footer() ?>
Now that your custom page template is ready (thanks to an elaborative yet simple method), go ahead and work your way around it.