wordpress怎么手动自定义文章显示数量
下面由/” target=”_blank”>WordPress技巧栏目给大家介绍wordpress手动自定义文章显示数量的方法详解,希望对需要的朋友有所帮助! 每页显示的文章数在后台阅读设置中指定,会应用到blog列表页(通常是首页)、搜索页、标签页、分类页以及时间索引页面,加入这些页面的结构不尽相同,例如有的显示标题和摘要,有的只显示标题,那么指定相同的页数就不适用于每个页面了。要根据页面类型指定每页显示文章数,需要写代码实现。 推荐的方法 修改每页显示的文章数也就是修改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' ); 登录后复制 缺点: 第一,增加查询次数…