[英]Wordpress: Limit posts per page in category
在我的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. ?>
我試圖用if(have_posts()) : while (have_posts()) : the_post(); ?> [...]
更改行if(have_posts()) : while (have_posts()) : the_post(); ?> [...]
在category.php中使用if(have_posts()) : while (have_posts()) : the_post(); ?> [...]
,使其類似於index.php中的那一行,但是我沒有嘗試過。
WordPress對此有一個設置,可以在管理區域的“ 設置”->“閱讀”->“博客”頁面中最多顯示
您可以使用它而不是自定義修改查詢。 這可能會使得在將來維護項目變得容易一些。
使用posts_per_page
參數( 此處為Codex )
$args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
在第一個示例中,實際上有兩個查詢: new WP_query
,然后是query_posts
,因此您需要擺脫其中的一個,因為這是多余的。 在第二個示例中,恰恰相反,您沒有任何查詢(盡管WordPress可能默認執行一個查詢,具體取決於調用此頁面的位置)。 因此,無論如何,在第二個示例中使用$showposts
是沒有意義的,因為在...之后, if (have_posts())
通常用於處理默認循環(在頁面代碼中不可見if (have_posts())
,則不會執行查詢。 WordPress或對待您之前聲明的查詢(通常使用query_posts()
)。 就像@Samuel所說的那樣,要使用的參數是posts_per_page
,但是我認為您還不存在,因此您應該首先開始學習如何執行查詢,因此您可以通過閱讀query_posts
上的WordPress Codex開始,它將是最好的最佳去處: http : //codex.wordpress.org/Function_Reference/query_posts 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.