修改每页显示的文章数也就是修改posts_per_page参数,将下面的代码放到functions.php中即可实现,代码来自WordPress Answers。
function custom_posts_per_page($query){ if(is_home()){ $query->set('posts_per_page',8);//首页每页显示8篇文章 } if(is_search()){ $query->set('posts_per_page',-1);//搜索页显示所有匹配的文章,不分页 } if(is_archive()){ $query->set('posts_per_page',25);//archive每页显示25篇文章 }//endif }//function //this adds the function above to the 'pre_get_posts' action add_action('pre_get_posts','custom_posts_per_page');
通过WordPress的条件标签,你可以任意扩展这段代码。
不推荐直接修改主题模板,例如在index.php主循环前用query_posts更改每页显示文章数目
query_posts( 'posts_per_page=5' );<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
缺点:
第一,增加查询次数
第二,灵活度不高,如果分类、标签有自己的模板,还需要到那些模板里重复query_posts的把戏。
第三,query_posts使用时需特别小心,如果忘记恢复全局变量,可能会出现莫名其妙的错误。
必须登录才可以留言哦!