admin管理员组文章数量:1391767
I have tried this code, but it doesn't work.
function university_adjust_queries($query) {
$query->set('post_per_page', '1');
}
add_action('pre_get_posts', 'university_adjust_queries');
I have tried this code, but it doesn't work.
function university_adjust_queries($query) {
$query->set('post_per_page', '1');
}
add_action('pre_get_posts', 'university_adjust_queries');
Share
Improve this question
edited Feb 6, 2020 at 21:26
Hector
6821 gold badge7 silver badges18 bronze badges
asked Feb 5, 2020 at 20:00
ChrisChris
1
1
- You can change how many posts appear on archives in the settings in WP Admin, you don't need a filter for it – Tom J Nowell ♦ Commented Feb 5, 2020 at 21:26
2 Answers
Reset to default 3You're missing an s.
$query->set('posts_per_page', '1');
However - you probably don't want to do this for every query, since that affects everything - widgets, back end, everything. You should make it conditional. For example:
<?php
function university_adjust_queries($query) {
// If this is the main query, not in wp-admin, and this is the homepage
if($query->is_main_query() && !is_admin() && $query->is_home()) {
$query->set('posts_per_page', '1');
}
}
add_action('pre_get_posts', 'university_adjust_queries');
?>
This will make sure you're only affecting the main query (The Loop) on the homepage, and only on the front end. You can adjust the condition to target whichever spot you're wanting to affect.
function university_adjust_queries($query) {
if (!is_admin() AND is_post_type_archive('event') AND $query->is_main_query()) {
$query->set('posts_per_page', 1);
}
本文标签: queryHow to change the amount of posts previewed on a page
版权声明:本文标题:query - How to change the amount of posts previewed on a page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744771710a2624378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论