• Hi,

    Apologies if this topic has been covered but I’ve not had much luck in finding a working solution.

    I’m using a custom loop to pull in various different custom post types and I have it set up to add different classes to the first post on the front page, like so…

    <?php
    
                            $first_page_post_count = 11;
                            $subsequent_pages_post_count = 10;
                            $paged = $paged ? $paged : 1;
    
                            if($paged > 1){
                            // not first page
                            $posts_per_page = $subsequent_pages_post_count;
    
                            if($paged == 2){
                            // second page
                            $offset = $first_page_post_count;
                            } else {
                            // subsequent pages
                            $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
                            }
    
                            } else {
                            // first page
                            $offset = 0;
                            $posts_per_page = $first_page_post_count;
                            }
    
                            $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
                            $query_args = array(
                            'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
                            'posts_per_page' => $posts_per_page,
                            'offset' => $offset,
                            'paged' => $paged
                            );
    
                            $the_query = new WP_Query( $query_args );
                        ?>
    
                        <?php $counter = 0; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                        <div class="col push--bottom <?php if(!is_paged() && ($counter==0)) { echo 'xsmall-12 featured-article'; $counter++; } else { echo 'xsmall-12 small-6 article-preview'; } ?> ">

    That’s all of my code before the actual content. I have tried the suggested pagination options on the WordPress codex and can’t seem to get it work. The one I’ve tried out is this…

    <?php
    //Protect against arbitrary paged values
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    
    $args = array(
    	'posts_per_page' => 5,
    	'category_name' => 'gallery',
    	'paged' => $paged,
    );
    
    $the_query = new WP_Query( $args );
    ?>

    But it either break the loop entirely or does nothing, depending on where I place it.

    Any pointers would be much appreciated!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Could you try this code?

    <?php
      // Arguments setup for custom query
      // Make sure the category_name and post_type are correct (no typos)
      $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
      $query_args = array(
        'post_type' => 'post',
        'category_name' => 'tutorials',
        'posts_per_page' => 10,
        'paged' => $paged
      );
      // Create a new instance of WP_Query
      $the_query = new WP_Query( $query_args );
    ?>

    Hi,

    Can you try this:

    
    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $category_Name = "gallery";
    $category_Id = get_cat_ID( $category_Name );
    $args= array(
    'cat' => $category_Id,
    'posts_per_page' => 10,
    'paged' => $paged
    );
    $custom_query  = new WP_Query($args);
        if ($custom_query->have_posts()) {
            while ($custom_query->have_posts()) { 
                $custom_query->the_post();           
                //code statements
            }?>
            <div class="alignleft">
                  <?php next_posts_link('« Older Entries', $custom_query->max_num_pages) ?>
            </div>		
            <div class="alignright">
                   <?php previous_posts_link('Newer Entries »') ?>
            </div>	
        <?php
        }
        wp_reset_postdata();
    ?>
    

    Regards,
    Karan

    • This reply was modified 7 years, 11 months ago by nuancedesignstudio. Reason: extra spaces
    Thread Starter a_mulg

    (@a_mulg)

    Hi @dipakcg ,

    I tried your suggestion and couldn’t get it to work, though wasn’t entirely sure how to implement it. I tried pasting it before my loop in place of my current query and it didn’t return any posts. Where specifically in my template should I put your code?

    Thread Starter a_mulg

    (@a_mulg)

    Hi @nuancedesignstudio,

    I tried your suggestion too, replacing my own query with your code. The pagination did actually work in as far as I could see the ‘older entries’ link and use it to go back a page, however none of my posts were actually visible. I think I’m probably putting it in the wrong place.

    Thread Starter a_mulg

    (@a_mulg)

    Ok I now have numbered pagination displaying and it changes the page url (eg. localhost/page/2 etc.) however the same posts just load on each page. My current loop is…

    <?php
    
                            $first_page_post_count = 11;
                            $subsequent_pages_post_count = 10;
                            $paged = $paged ? $paged : 1;
    
                            if($paged > 1){
                            // not first page
                            $posts_per_page = $subsequent_pages_post_count;
    
                            if($paged == 2){
                            // second page
                            $offset = $first_page_post_count;
                            } else {
                            // subsequent pages
                            $offset = $first_page_post_count + ($subsequent_pages_post_count * ($paged - 2));
                            }
    
                            } else {
                            // first page
                            $offset = 0;
                            $posts_per_page = $first_page_post_count;
                            }
    
                            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                            $query_args = array(
                            'post_type' => array ('post' , 'reviews' , 'trailer' , 'feature'),
                            'posts_per_page' => $posts_per_page,
                            'offset' => $offset,
                            'paged' => $paged
                            );
    
                            $the_query = new WP_Query( $query_args );
                        ?>
    
                        <?php $counter = 0; if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                        <div class="col push--bottom <?php if(!is_paged() && ($counter==0)) { echo 'xsmall-12 featured-article'; $counter++; } else { echo 'xsmall-12 small-6 article-preview'; } ?> ">
    
                            //content
    
                        <?php $counter++;
                            if ($counter % 2 == 0) {
                                echo '</div><!-- end of row -->
                                <div class="row js-equal-height">';
                            }
                        endwhile; ?>
    
                        <?php else: ?>
                            <article>
                                <h1>Sorry...</h1>
                                <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
                            </article>
                        <?php endif; ?>
    
                        <?php if (function_exists("pagination")) {
                            pagination($the_query->max_num_pages);
                        } ?>
    
                        <?php wp_reset_postdata(); ?>

    And my pagination function in my functions file looks like this…

    function pagination($pages = '', $range = 4)
    {  
         $showitems = ($range * 2)+1;  
     
         global $paged;
         if(empty($paged)) $paged = 1;
     
         if($pages == '')
         {
             global $wp_query;
             $pages = $wp_query->max_num_pages;
             if(!$pages)
             {
                 $pages = 1;
             }
         }   
     
         if(1 != $pages)
         {
             echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
             if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
     
             for ($i=1; $i <= $pages; $i++)
             {
                 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
     
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";  
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
             echo "</div>\n";
         }
    }

    Any guidance on why it’s just returning the same posts would be much appreciated!

    Hi,

    So I am not sure why loop is returning the same post. You did not see posts using my code because you have to replace this line “//code statements” with actual code to format and display posts. example below:


    while ($custom_query->have_posts()) {
    $custom_query->the_post();
    //code statements
    the_content();
    }?>

    Karan

    Thread Starter a_mulg

    (@a_mulg)

    Hi @nuancedesignstudio,

    Thanks for your reply.

    You’re right about the content, so I have added that in and the posts display. However, it has had ultimately the same result as the code I posted above in that I can move back to older pages, but they still just show the same posts as the first page.

    Hi,

    The issue of the same post of all pages seems to be related with the pagination variable. Here’s a nice article for custom loops with pagination.
    http://callmenick.com/post/custom-wordpress-loop-with-pagination

    Perhaps you try a simple custom loop with just the basic filters and get the pagination to work, and then add in your functions and code.

    Karan

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Pagination for a custom loop’ is closed to new replies.