pagination

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #444
    Anonymous
    Guest

    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.

    #1139
    admin
    Participant

    Hello,

    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.

    Step 1:

    – Copy index.php and rename it to template-category.php

    Step 2:

    Copy the following code and paste it to the template-category.php

    [PHP]<?php
    /*
    Template Name: Category Template
    */

    /** Header */
    get_header();
    ?>

    <?php
    /** 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() ) : ?>

    <?php
    /** Catgory Loop */
    while ( $wp_query->have_posts() ) : $wp_query->the_post();

    /** More Tag Fix */
    global $more;
    $more = 0;
    ?>

    <?php
    /** Restore Post Data */
    wp_reset_postdata();
    endif;
    ?>

    <?php
    /** Restore Query */
    $wp_query = null;
    $wp_query = $temp_wp_query;
    ?>

    [/PHP]

    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.

    Enjoy!

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.