0

I am developing a Custom wordpress theme. In my home page I have created a custom loop to show latest posts in a grid view.. I am trying to add a pagination there and the pagination shows also. But the pagination is loading same posts grids every time. This loop has also counter added where I wanted to add advertisement grid into a certain position after a grid..

This is the grid link https://dev.ahsanurrahman.com/myprojects/cms/wp/csdug/

My Code is as below and I am using index.php as a front page to show this posts grid…

<?php
/**
 * The main template file
 */

get_header(); ?>

<?php // Advertisement Placeing Number Functions ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$place_of_first_add_in_home_page = ot_get_option( 'place_of_first_add_in_home_page', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$place_of_second_add_in_home_page = ot_get_option( 'place_of_second_add_in_home_page', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$place_of_third_add_in_home_page = ot_get_option( 'place_of_third_add_in_home_page', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$place_of_fourth_add_in_home_page = ot_get_option( 'place_of_fourth_add_in_home_page', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$place_of_fifth_add_in_home_page = ot_get_option( 'place_of_fifth_add_in_home_page', '' );} ?>

<?php // Advertisement Block Functions ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$p300x250__1__home = ot_get_option( 'p300x250__1__home', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$p300x250__2__home = ot_get_option( 'p300x250__2__home', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$p300x250__3__home = ot_get_option( 'p300x250__3__home', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$p300x250__4__home = ot_get_option( 'p300x250__4__home', '' );} ?>

<?php if ( function_exists( 'ot_get_option' ) ) {$p300x250__5__home = ot_get_option( 'p300x250__5__home', '' );} ?>

<?php // Advertisement Block Functions ?>

    <div class="main-content">
        <div class="main-wrap">

            <div class="col-md-12 hero-post">



            <?php query_posts('order=dsc&showposts=1'); ?>

            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

             <div class="col-md-12">

             <div class="col-md-9">

                <div class="hero_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->

            </div><!-- col-md-9 ends here -->


            <div class="col-md-3">

                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>

             </div><!-- col-md-3 ends here -->



          </div><!-- col-md-12 ends here -->


                <?php endwhile; ?>



        <?php endif; ?>


            </div><!-- hero post ends here -->












            <div class="col-md-12">

            <?php 

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

                  $query_args = array(
                      'post_type' => 'post',
                      'posts_per_page' => 3,
                      'paged' => $paged,
                      'page' => $paged,
                      'offset' => 1
                    );

                  $the_query = new WP_Query( $query_args ); ?>

                  <?php $adcounter = 1; ?>

            <?php if ( $the_query->have_posts() ) : ?>

            <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>


            <?php if ($adcounter == $place_of_first_add_in_home_page) : ?> 


            <?php {?>

            <div class="col-md-4 home-post-grid post-container">

            <?php echo $p300x250__1__home;?>


            </div><!-- advertisement area ends here -->

             <div class="col-md-4 home-post-grid">

                <div class="other_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->


                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>



                </div><!-- home post grid ends here -->


<?php }?>

<?php elseif ($adcounter == $place_of_second_add_in_home_page) :{ ?>

            <div class="col-md-4 home-post-grid">

            <?php echo $p300x250__2__home;?>


            </div><!-- advertisement area ends here -->

             <div class="col-md-4 home-post-grid">

                <div class="other_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->


                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>



                </div><!-- home post grid ends here -->

<?php } ?>

<?php // Second Else if ends here ?>

<?php elseif ($adcounter == $place_of_third_add_in_home_page) :{ ?>

            <div class="col-md-4 home-post-grid">

            <?php echo $p300x250__3__home;?>


            </div><!-- advertisement area ends here -->

             <div class="col-md-4 home-post-grid">

                <div class="other_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->


                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>



                </div><!-- home post grid ends here -->


<?php } ?>

<?php // 3rd Else if ends here ?>

<?php elseif ($adcounter == $place_of_fourth_add_in_home_page) :{ ?>

            <div class="col-md-4 home-post-grid">

            <?php echo $p300x250__4__home;?>


            </div><!-- advertisement area ends here -->

             <div class="col-md-4 home-post-grid">

                <div class="other_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->


                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>



                </div><!-- home post grid ends here -->

<?php } ?>

<?php // 4th Else if ends here ?>

<?php elseif ($adcounter == $place_of_fifth_add_in_home_page) :{ ?>

            <div class="col-md-4 home-post-grid">

            <?php echo $p300x250__5__home;?>


            </div><!-- advertisement area ends here -->


             <div class="col-md-4 home-post-grid">

                <div class="other_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->


                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>



                </div><!-- home post grid ends here -->


<?php } ?>

<?php // 5th Else if ends here ?>

<?php else : ?>

<?php {?>


             <div class="col-md-4 home-post-grid">

                <div class="other_post_thumbnail">

            <a href="<?php the_permalink();?>"><?php if ( ! post_password_required() && ! is_attachment() ) :
                the_post_thumbnail();
            endif; 


            ?></a>

            </div><!-- post thumbnail ends here -->


                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                <h5 class="csdug_subtitle"><?php global $post; echo get_post_meta($post->ID,'wpshed_textfield',true) ?></h5>
                <h3 class="entry-title"><?php the_title(); ?></h3></a>
                <div class="home-grid-post-info"><?php csdug_entry_meta(); ?></div>
                <div class="home-para"><?php the_excerpt();?></div>
                <div class="post_love_count"><?php echo do_shortcode('[post_love_count]');?></div>

                <div class="clr"></div>
                 <?php echo do_shortcode('[social_share_icons]');?>

                 <div class="clr"></div>



                </div><!-- home post grid ends here -->

<?php }?>

            <?php endif; ?>


            <?php $adcounter++; ?>

            <?php endwhile; ?>



            </div> <!-- row ends here -->


              <?php

      if (function_exists(custom_pagination)) {
        custom_pagination($the_query->max_num_pages,"",$paged);
      }
    ?>



    <?php wp_reset_postdata(); ?>



            </div> <!-- col-md-12 ends here -->


                <?php endif; ?>



            </div><!-- main wrap ends here -->

        <div class="clr"></div>

    </div><!-- main content ends here -->


<?php get_footer(); ?>

And here is the custom pagination code in function.php

<?php // Custom Pagination for loop
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
  if (empty($pagerange)) {
    $pagerange = 2;
  }
  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   * 
   * It's good because we can now override default pagination
   * in our theme, and use this function in default quries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }
  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );
  $paginate_links = paginate_links($pagination_args);
  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }
} ?>
  • Have you verified that $paged contains the correct page number? – Milo Oct 14 '17 at 22:24
  • down voted for the usage of query_posts. There is no point in asking anything and expecting any reasonable answer when you use that monster, especially when you are asking about pagination. Use pre_get_posts filter instead – Mark Kaplun Oct 15 '17 at 5:15
  • @mark I have used that to create the hero post only.. if you look there is the main query after hero post for which I have used WP_Query.. for that loop only I am asking pagination.. that is the grid. – Md. Amanur Rahman Oct 15 '17 at 23:48
  • @Md.AmanurRahman Did you try using the page instead of paged, as in my answer? It might not be the preferred way to do it (which is why Mark downvoted it) but it should work if you are continuing to use WP_Query. – FluffyKitten Oct 16 '17 at 8:55
  • @FluffyKitten Yes I tried that and even everything.. I think the loop has a problem a bit.. That is what I am not getting.. I think MArk Kaplun Is right but just not getting how to set the lopp. – Md. Amanur Rahman Oct 16 '17 at 12:49
2

Between your question and comments, you say that the purpose of changing the main query is to exclude the most recent post from the main loop on your static home page, becuase it is being displayed as a "hero" post on its own before the rest.

The easiest way to do this is actually using the main loop, and use the pre_get_posts action and found_posts filter to adjust it to remove the most recent post, as follows:

1. Index.php: Get the most recent post(s) using WP_Query

Use a custom query to get the most recent post

<?php 
// custom query to get the most recent post
$the_query = new WP_Query( array('posts_per_page' => '1', 'order' => 'DESC') );

if( $the_query->have_posts() ): 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        global $post;  ?>

        [...display post content as required, e.g...]
        <h1><a href="<?php the_permalink();?>"><?php the_title();?></a></h1>
        <?php the_content(); ?>

    <?php endwhile;
endif; 
wp_reset_query();    // Restore global post data stomped by the_post(). 
?>


2. Functions.php: Set up offset and pagination to remove the most recent post(s) from the main loop

Hook into pre_get_posts to adjust the query and pagination for the number of posts to skip, and found_posts filter to adjust the value of number of posts found. Add to functions.php:

/**  Exclude the most recept post from the main query  **/
add_action('pre_get_posts', 'pre_query_homepage_offset', 1 );
function pre_query_homepage_offset(&$query) {

    // only continue if this is the main query on the homepage
    if ( !$query->is_home() || !$query->is_main_query() ) 
        return;

    $posts_to_skip = 1;  // the number of most recent posts to skip
    $posts_per_page = get_option('posts_per_page'); // get posts per page from WP settings

   //Detect and handle pagination...
     if ( !$query->is_paged ) // if we're on the first page...
        $offset = $posts_to_skip; //...just offset by the number of posts to skip
    else 
        // Calculate the page offset if we are not on the first page
        $offset = $posts_to_skip + ( ($query->query_vars['paged']-1) * $posts_per_page );

    $query->set('offset',$offset);  // set the offset for the query
}


/**  Adjust the number of posts found by the main query to account for our offset **/
add_filter('found_posts', 'adjust_homepage_offset_pagination', 1, 2 );
function adjust_homepage_offset_pagination($found_posts, $query) {

    // return the actual value if this isn't the main query on the homepage
    if ( !$query->is_home() || !$query->is_main_query() ) 
        return $found_posts;

    $posts_to_skip = 1; // the number of posts to skip
    return $found_posts - $posts_to_skip;
}


3. Index.php: Display the rest of the posts (excluding the most recent) using the main loop

Instead of using a custom query, now we can use the main loop, as it now has been adjusted to exclude the most recent post by our functions in step 2.

<?php if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); ?>

        [...display post content as required e.g....]
        <h2><?php the_title( ); ?></h2>
        <?php the_content(); ?>

    endwhile;

    // Previous/next page navigation.
    the_posts_pagination( array(
        'prev_text'  => __('&laquo;'),
        'next_text'  => __('&raquo;'),
    ) );

endif;
?>
  • Ok.. Let me try with this – Md. Amanur Rahman Oct 17 '17 at 8:01
  • Ok. Now getting this Error like below call_user_func_array() expects parameter 1 to be a valid callback, function 'myprefix_query_offset' not found or invalid function name – Md. Amanur Rahman Oct 17 '17 at 8:18
  • @ FluffyKitten Woho Man.. You are great.. This really works.. Thanks for help. Now the pagination working really great... Thanks thanks thanks. Though I will be adding now my ajax pagination.. Thank you very much.. you solved my problem – Md. Amanur Rahman Oct 17 '17 at 11:33
  • @Md.AmanurRahman Glad to help! I'd an error in the code: I changed the function names but forgot to change them in add_action & add_filter, but it looks like you found the problem. I've updated it my answer anyway. P.S. I'm a woman, not a man :) Good luck with the ajax pagination! – FluffyKitten Oct 17 '17 at 23:37
  • But now having one problem.. The first post is skipping from hero posts as well.. So it is skipping the first post from the main query and from the hero post query also. So it's ultimately same post showing in hero and in the grid again... Any idea ? though looking into this – Md. Amanur Rahman Oct 18 '17 at 5:33
0

If you need an "inner loop" pagination, you should use your own query variable to "carry" the number of the requested page. This means you can not use the wordpress APIs for generating pagination links, and will have to write your own.

  • Would you mind to give me the correct code please for which the pagination will work ? – Md. Amanur Rahman Oct 15 '17 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.