Support » Fixing WordPress » Pagination not working on custom template.

  • I am trying to utilize the pagination feature on my website, as can be seen here: http://ohmybonbon.com

    However, when the page numbers are clicked on, the content does not change…

    Here is my index code:

    http://pastebin.com/qy2UEqEG

    I have it set to display 9 posts per page, but again, as stated above, regardless of what page number I click on, the first posts 1-9 are the only ones that are displayed.

    I am sure the solution is painfully simple, but I am not having any luck with anything I have tried, thus far.

    Thanks for any help!
    -Ashley

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Do you use a query on the loop?

    Well, you can see everything I have up in the loop there in the pastebin link, but this is my main query:

    <?php
    if (is_home()) {
    query_posts("cat=-94"."&posts_per_page=9");}?>
    <?php $c = 1; //init counter
    $bpr = 3; //boxes per row
    if(have_posts()) :
    	while(have_posts()) :
    		the_post();?>
    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try to do it without the query_posts:

    But with something like this in your functions.php

    function my_home_query( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_home()){
         //cat=-94"."&posts_per_page=9
          $query->set('cat', -94);
          $query->set('posts_per_page', 9);
    
        }
      }
    }
    add_action( 'pre_get_posts', 'my_home_query' );

    Basically it’s more reliable and faster. read here why: http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

    Aha! You are a genius! Thanks so much for this! You saved me countless hours, and headaches. E-cookie?

    Just one (okay, two) things before you go –

    I am using this below to generate the pagination:

    <?php global $wp_query;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );?>

    Will this (and your fix) also work on all of my custom Page templates?
    And, can the Next Previous links be changed to images?

    Thanks so much!

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    By custom page templates you mean this with a query_posts in it?
    http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

    let me see about the images.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Put a folder called “images” with the images in it in your theme in a folder .

    Add this to the paginate_links arguments array:

    'next_text' => '<img src="'.get_template_directory_uri().'/images/next.png" alt="previous page" />',
    'prev_text' => '<img src="'.get_template_directory_uri().'/images/previous.png" alt="next page" />',

    in this example the images are called “next.png” and “previous.png”.

    http://codex.wordpress.org/Function_Reference/paginate_links

    Yes, that’s exactly right. For instance, every link you see on my navigation (http://ohmybonbon.com) utilizes a custom page template.

    So, for my page template in the “Beauty” cagetory, I am using this query:

    <?php query_posts('category_name='.beauty.','.makeup.'&post_status=publish,future');?>

    So, I have a feeling this isn’t quite going to work the same. I’m sure the solution is probably easy, and staring me in the face, but I’m a little brain dead this morning.

    You have been a tremendous help! I will try adding the previous and next arguments.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    If you don’t use the main loop to show the content of the Page I would use a new WP_Query in stead of a query_posts.
    http://codex.wordpress.org/Function_Reference/WP_Query

    example loop:

    $the_query = new WP_Query( 'category_name='.beauty.','.makeup.'&post_status=publish,future' );
    if($the_query->have_posts()) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
    // rest of loop
    endwhile;
    endif;

    But, this means you have to change the paginate_links arguments [untested]:

    <?php 
    
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
    
        //  I changed this
        'total' => $the_query->max_num_pages
    ) );?>

    On all other pages (category.php, search.php etc..) where you want to alter the main loop try it with the pre_get_post action hook with conditional tags in functions.php

    keesiemeijer, you have been a tremendous help! I have everything pretty well how I want it, now. I am going to tamper with the pagination style a bit, but it works splendidly now, thanks to you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Pagination not working on custom template.’ is closed to new replies.