WordPress.org

Ready to get started?Download WordPress

Forums

Twenty Twelve
[resolved] Paging fails when custom template used for home page (2 posts)

  1. shochatd
    Member
    Posted 8 months ago #

    I have created a custom page template which I believe would be considered a "showcase" type in that it allows static text, but when viewed, includes a post stream (in all but one excluded category, specified using a custom variable). This template works correctly, including paging of the post stream. However, as soon as I designate the page to be my home page, the paging stops working: I get the 4 most recent posts (plus a sticky one) but when I click on "<-- Older posts", I do see "/page/2" appended to the URL (which is the site URL since this is the home page) but I still get only the same set of recent posts.
    Site URL: http://allsaintschelms-new.dreamhosters.com/
    Here is the code for the template:

    <?php
    /*
    Template Name: X-Category
    */
    
    /* This example is for a child theme of Twenty Twelve:
    *  You'll need to adapt it the HTML structure of your own theme.
    */
    
    get_header(); ?>
    
    	<div id="primary" class="content-area">
    		<div id="content" class="site-content" role="main">
            <?php
            /* The loop: the_post retrieves the content
             * of the new Page you created to list the posts,
             * e.g., an intro describing the posts shown listed on this Page..
             */
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
                  // Display content of page
                  get_template_part( 'content', get_post_format() );
                  wp_reset_postdata();
    
                endwhile;
            endif;
    
        $category = get_post_meta( $posts[0]->ID, 'category', true );
        $cat = get_cat_ID( $category );
    
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $post_per_page = 4; // -1 shows all posts
        $do_not_show_stickies = 0; // 0 to show stickies
        $args=array (
          'category__not_in' => array( $cat ),
          'orderby' => 'date',
          'order' => 'DESC',
          'paged' => $paged,
          'posts_per_page' => $post_per_page,
          'ignore_sticky_posts' => $do_not_show_stickies
        );
    
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query( $args );
        ?>
        <?php if ( $wp_query->have_posts() ) : ?>
    
                     <div class="catsec">
    			<?php global $more; /* The loop */ ?>
    			<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
    				<?php $more = 0; // Display content of posts ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php endwhile; ?>
    
    			<?php twentytwelve_content_nav( 'nav-below' ); ?>
    
    		<?php else : ?>
    			<?php get_template_part( 'content', 'none' ); ?>
    		<?php endif; ?>
    
                     </div><!-- .catsec -->
    
    		</div><!-- #content -->
    
        <?php
        $wp_query = null;
        $wp_query = $temp;
        ?>
    
        </div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. shochatd
    Member
    Posted 8 months ago #

    I just found the solution here:
    http://stackoverflow.com/questions/13768900/wordpress-pagination-not-working
    The crucial fix was to change this:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    to this:

    if ( get_query_var('paged') ) {
          $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
          $paged = get_query_var('page');
        } else {
          $paged = 1;
        }

    The original, simpler version works as long as this is not the home page, but for the home page, only the version that checks 'page' works.

Reply

You must log in to post.

About this Theme

About this Topic