level 3
lansk08
楼主
请都一下,pre_get_posts 这个需在 functions.php 里面添加代码吗?
我都是在在主页直接用的<?php query_posts('tag=房价&showposts=5&offset=0'); ?>后面是<?php while (have_posts()) : the_post(); ?>
像这样.
但是WP教程上面说的下面的内容:
我们强烈建议您使用 pre_get_posts 来替代 query_posts() ,同时使用 is_main_query 来检查主查询。
举个例子,在首页(homepage),你可以看到最新的10篇文章。如果你想仅仅显示 5 篇文章(不在乎分页),你可以像这样使用 query_posts() :
1 query_posts( 'posts_per_page=5' );
下面是在 functions.php 中使用 pre_get_posts 实现类型的结果:
function five_posts_on_homepage( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'posts_per_page', '5' ); }}add_action( 'pre_get_posts', 'five_posts_on_homepage' );
请教下,上面的代码是直接加在<?php while (have_posts()) : the_post(); ?>这个前面还是加在functions.php里面,谢谢
2019年08月02日 10点08分
1
我都是在在主页直接用的<?php query_posts('tag=房价&showposts=5&offset=0'); ?>后面是<?php while (have_posts()) : the_post(); ?>
像这样.
但是WP教程上面说的下面的内容:
我们强烈建议您使用 pre_get_posts 来替代 query_posts() ,同时使用 is_main_query 来检查主查询。
举个例子,在首页(homepage),你可以看到最新的10篇文章。如果你想仅仅显示 5 篇文章(不在乎分页),你可以像这样使用 query_posts() :
1 query_posts( 'posts_per_page=5' );
下面是在 functions.php 中使用 pre_get_posts 实现类型的结果:
function five_posts_on_homepage( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'posts_per_page', '5' ); }}add_action( 'pre_get_posts', 'five_posts_on_homepage' );
请教下,上面的代码是直接加在<?php while (have_posts()) : the_post(); ?>这个前面还是加在functions.php里面,谢谢