Pagination with custom loop

The question:

my question may look like Pagination not working with custom loop, but there is a different. I use the custom loop for display flash game. I want to make a pagination on page games by category. category.php :

<?php
if ($cat)
{
$cols = 2;
$rows = 4;
$paged = (('paged')) ? get_query_var('paged') : 1;
$post_per_page = $cols * $rows; // -1 shows all posts
$do_not_show_stickies = 1; // 0 to show stickies

$args = array(
    'post_type' => 'game',
    'category__in' => array($cat),
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $paged,
    'posts_per_page' => $post_per_page,
    'caller_get_posts' => $do_not_show_stickies
);
$wp_query = new WP_Query($args); 
begin_roundblock(get_cat_name($cat), 'games-pages-category', null);
if (have_posts()):
  echo '<div class="games-list-block-content">';
    /* Begin Breadcrump*/
    echo '<div class="breadcrumb">';
            if(function_exists('bcn_display'))
            {
                echo '<div class="breadcrumb-text">';
                echo 'Go Back:';
                    bcn_display();
                echo '</div>';
            }
    echo '</div>';
    /* End Breadcrump*/
    $i = 0;
    while (have_posts())
    {
        the_post();

        $class = 'game-info';
        if ($i % $cols == 0)
            $class .= ' clear';

        echo '<div class="'.$class.'"><a href="'.get_permalink().'" rel="nofollow noreferrer noopener">';
        the_post_thumbnail(array(60, 60), array('class' => 'game-icon'));
        $title = get_the_title();
        if (mb_strlen($title) > 7)
            $title = mb_substr($title, 0, 6).'...';
        echo '<span class="game-title">'.$title.'</span></a></div>';
        $i++;
    } ?>
     <div class="navigation clear game-info-last-row">
<?php if(function_exists('wp_pagenavi')) 
          { wp_pagenavi(); } ?>
</div>
//For default WP
 <div class="navigation">
  <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
  <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>

  </div>
<?php else: ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isnt here.</p>
    <?php get_search_form(); ?>
<?php endif;
end_roundblock();
}
?>

I got the pagination links, also I tried to use the wp-pagenavi plugin and it’s right calculate the number of my post’s(game’s) to display with right number of pages. But when I clicked on link “Older Entries” (or any page in case pagenavi plugin) it’s go to main page, but url is “http://mydomain/category/category_name/page/2“. I try to use many other plugin’s, but all the same. Doe’s anybody could help me with it?
Thanks.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

If this is the main loop you use to display posts on your page, you should not execute a new loop but modify the existing loop that WordPress will execute anyway. This way you can be sure that all extra query parameters will be taken into account.

Here we want to display posts of type game and limit the number of posts on the page. You can do this with the following code:

add_action( 'pre_get_posts', 'wpse5477_pre_get_posts' );
function wpse5477_pre_get_posts( &$wp_query )
{
    if ( $wp_query->is_category() ) {
        $wp_query->set( 'post_type', 'game' );
        $wp_query->set( 'posts_per_page', 2 );
    }
}

Method 2

Looks like youa re using wp-pagenavi from Lester Chan ?

Then just do this:

$page = (get_query_var('paged')) ? get_query_var('paged') : 1; // U don't need this for normal WP paging.

 if(function_exists('wp_pagenavi')) 
              { wp_pagenavi(); }

Otherwise you can use default WP paging:

<div class="navigation">
  <span class="floatLeft"><?php next_posts_link('&laquo; Older Entries') ?></span>
  <span class="floatRight"><?php previous_posts_link('Newer Entries &raquo;') ?></span><div class="clear"></div>
</div> 


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment