WordPress.org

Ready to get started?Download WordPress

Forums

Pagination not working on custom template. (10 posts)

  1. Ashley Michèlle
    Member
    Posted 2 years ago #

    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

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Do you use a query on the loop?

  3. Ashley Michèlle
    Member
    Posted 2 years ago #

    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();?>
  4. keesiemeijer
    moderator
    Posted 2 years ago #

    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/

  5. Ashley Michèlle
    Member
    Posted 2 years ago #

    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!

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    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

  8. Ashley Michèlle
    Member
    Posted 2 years ago #

    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.

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    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

  10. Ashley Michèlle
    Member
    Posted 2 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic