Wordpress:限制每页的帖子数量

时间:2012-11-19 22:33:20

标签: php wordpress categories

在我的index.php中,我使用此代码来限制每页的帖子,并且它可以正常运行:

$showposts = 5;
$do_not_show_stickies = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);

$loop2query = new WP_Query($args);

query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?>

<div class="blogpost"> ... </div>

<?php endwhile; endif;
posts_nav_link(); // Navigating the pages with $showposts each. ?>

相同的代码在category.php中不起作用,所以我将其更改为以下内容但它仍然不起作用:

$showposts = 4;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

if (have_posts()) { while (have_posts()) { the_post(); ?>
    <div class="blogpost"> ... </div>

<?php } }

else { ?>
    <p>There are no blog posts in this category.</p>
<?php } ?>

<?php posts_nav_link(); // Navigating the pages with $showposts each. ?>

我尝试使用category.php中的if(have_posts()) : while (have_posts()) : the_post(); ?> [...]更改行,使其与index.php中的那行相似,但我尝试过没有尝试过。

3 个答案:

答案 0 :(得分:5)

Wordpress有一个设置,可在设置 - &gt;下的管理区域找到阅读 - &gt;博客页面最多显示

您可以使用此功能代替自定义修改查询。这可能会让你的项目更容易维持下去。

答案 1 :(得分:0)

使用posts_per_page参数(Codex here

$args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);

答案 2 :(得分:0)

在第一个示例中,您实际上有两个查询:new WP_query,然后是query_posts,因此您需要删除其中一个,因为这是多余的。在第二个例子中,相反,你没有任何查询(虽然WordPress可能默认执行一个,具体取决于调用此页面的位置)。所以无论如何,在你的第二个例子中使用$showposts是没有意义的,因为你在...之后没有执行查询。 if (have_posts())通常用于处理来自WordPress的默认(在页面代码中不可见)循环或处理您之前声明的查询(通常使用query_posts())。 正如@Samuel所说,使用的论点是posts_per_page,但我认为你还没有,你应该首先开始学习如何执行查询,这样你就可以开始阅读{{上的WordPress codex 1}},它将是最先走的地方:http://codex.wordpress.org/Function_Reference/query_posts