我的问题可能是Pagination not working with custom loop, 但有一种不同。我使用自定义循环来显示flash游戏。我想按类别在游戏页面上分页。类别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();
}
?>
我得到了分页链接,我还尝试使用wp pagenavi插件,它正确地计算了我的帖子(游戏)的数量,以显示正确的页数。但当我点击链接“旧条目”(或任何页面,如果是pagenavi插件)时,它会转到主页,但url是“
http://mydomain/category/category_name/page/2“。我尝试使用许多其他插件,但都一样。Doe有人能帮我吗?谢谢。
最合适的回答,由SO网友:Jan Fabry 整理而成
如果这是用于在页面上显示帖子的主循环,则不应执行新循环,而应修改WordPress将执行的现有循环。这样,您可以确保所有额外的查询参数都将被考虑在内。
这里我们要显示类型为game
并限制页面上的帖子数量。可以使用以下代码执行此操作:
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 );
}
}