Wordpress:限制每页的帖子数量

Kat*_*aty 0 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. ?>
Run Code Online (Sandbox Code Playgroud)

相同的代码在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. ?>
Run Code Online (Sandbox Code Playgroud)

我尝试if(have_posts()) : while (have_posts()) : the_post(); ?> [...]在category.php中更改该行,使其类似于index.php中的那一行,但我没有尝试过.

Sha*_*nig 5

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

您可以使用此方法而不是自定义修改查询.这可能会使您的项目更加容易.