Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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().'">';
        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.

share|improve this question
    
@glazsasha: Am I correct when I think you display this on a regular page? (The is_page() at the top?) So you have a page /the-history-of-arcade-games/, meta cat value is 2 (for arcade games), and you want to display (in a sidebar, or at the bottom) 4 posts from this category? In this case, should the next page link go to /the-history-of-arcade-games/page/2/, or /category/arcade/page/2/? Because the former is (by default) not supported by WordPress. –  Jan Fabry Dec 16 '10 at 11:29
    
I could apply the certain code, that I'm used. But I think there is no different how display list of game for some category. In the code of top after declaration $args it's default WP query –  glazsasha Dec 16 '10 at 12:03
    
@Jan Farby: Actually, I wrote this code just to display my custom loop (with 'post_type'=>'game'). I'm sorry if I misled you or someone's else. Now, i deleted the is_page() from code, it's my fault,sorry. Just to be clear: i have sidebar block with name's of games category on main page. Then I clicked on any category and have url looks like: mydomain/category/category_name. I see game from this category, when I click on any page(Lester Chan plugin) or "Older Entries"(default WP) it's go to mydomain/category/category_name/page/2. There is the problem. –  glazsasha Dec 16 '10 at 12:06
    
@glazsasha: It is perhaps clearer if you include the full (actual) code from the template file, and tell us which file it is (archive.php, category.php, ...). –  Jan Fabry Dec 16 '10 at 12:07
1  
@glazsasha: I'm sorry, but I don't think I understand your question. Is this the code for the sidebar only, or for the complete page? What do you want to show on the second page? What should the URL of the first and of the second page be? Maybe you should come to the chat room so we can try to clear this up. –  Jan Fabry Dec 16 '10 at 15:31

2 Answers 2

up vote 3 down vote accepted

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 );
    }
}
share|improve this answer
    
What should I do, if I want to display (for example) 5 posts from certain category on the main page? By this function we already determined post_per_page = 2 for posts from category. How could I achieve to display 2 post from certain category on the page category.php and 5 posts from the same category on the main page. Does anybody has some idea? @Jan Fabry, I want to ask in the chat room for this questions, but the room was frozen. –  glazsasha Jan 17 '11 at 14:56
    
@glazsasha: I think this is a new question. Are you talking about the homepage, or the first page of the category archive? –  Jan Fabry Jan 18 '11 at 7:31
    

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> 
share|improve this answer
    
Yes, I try to use wp-pagenavi from Lester Chan and do this way in case of default WP paging. Sorry, but I think the different in name's of class(class="floatLeft,class="floatRight) and &raquo;&laquo; don't resolve my promlem. As I wrote in my code: <div class="navigation"> <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div> </div> But thanks anyway! –  glazsasha Dec 16 '10 at 10:36
    
<span class="floatLeft"> is just a CSS class I use to float the text left. You can use whatever you want. It has nothing to do with the actuall paging. –  Steven Dec 16 '10 at 10:48
    
Huh... actually, I have the same problem as you have (more or less). norwegianfashion.no/archive . I can go to next page, but the same articles are listed. But here it works, using LEsetr Chan's plugin: norwegianfashion.no/magazine –  Steven Dec 16 '10 at 10:50
    
You're lucky! Lester Chan plugin can't help in my case. Now, I think maybe the problem is that i'm trying to use pagination for page games by category or there is no different? –  glazsasha Dec 16 '10 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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