December 2, 2013 at 10:19 pm #444AnonymousGuest
I created custom templates for each category page so that they could each have their own sidebars. This works fine except that the pagination is not working. If you go here http://edwarddufresne.com/trials and then to http://edwarddufresne.com/coddington/ you can see what I mean. This is what i put in and visually it is right but it is not working correctly.December 3, 2013 at 6:42 am #1139adminParticipant
I created custom templates for each category page so that they could each have their own sidebars. This works fine except that the pagination is not working.
This concept is about of programming nature. I will be happy to explain it here so it might be helpful for other users and developers as well.
– Copy index.php and rename it to template-category.php
Copy the following code and paste it to the template-category.php
Template Name: Category Template
/** Header */
/** Custom Category Query */
$paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1;
$args = array( ‘category__in’ => array( 4 ), ‘paged’ => $paged, ‘posts_per_page’ => 2 );
/** Hold $wp_query */
$temp_wp_query = $wp_query;
$wp_query = null;
/** Custom $wp_query */
$wp_query = new WP_Query( $args );
have_posts() ) : ?>
/** Catgory Loop */
while ( $wp_query->have_posts() ) : $wp_query->the_post();
/** More Tag Fix */
$more = 0;
/** Restore Post Data */
/** Restore Query */
$wp_query = null;
$wp_query = $temp_wp_query;
Customizable part of this template for you and developers who wish to implement category pagination in the WordPress templates perfectly.
1. ‘category__in’ => array( 4 )
Please replace with the category ID, you wish to display on this page. You may also use multiple category IDs separated by commas.
2. ‘posts_per_page’ => 2
You may set the number of posts to show on this template.
These settings can easily be made dynamic by using Meta boxes or via Options panel.
Important concepts about pagination in the WordPress templates
1. For Custom WordPress Templates, Always use WP_Query for secondary loops, and pre_get_posts to modify the main query, rather than query_posts().
2. Don’t forget to restore Post Data and Main Query at the end of your logic – if you are using WP_Query for secondary loops.
- You must be logged in to reply to this topic.