列出多个 $query->set 的正确方法是什么?我可以拥有多个$query->set
就像下面的“代码 A”?
或者我应该将它们合二为一?如果是,您将如何将这三者结合起来$query->set
代码?我尝试将它们组合在下面的“代码 B”中,但没有成功。
.
代码 A:在组合 $query->set 之前
这段代码位于functions.php中
function featured_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'new' );
$query->set( 'cat', '-60, -61' );
$query->set( 'posts_per_page', 5 );
}
}
add_action( 'pre_get_posts', 'featured_category' );
.
代码 B:组合 $query->set 后
这段代码位于functions.php中
function featured_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( array( 'category_name' => 'new', 'cat' => '-60, -61', 'posts_per_page' => 5) );
}
}
add_action( 'pre_get_posts', 'featured_category' );
正确的使用方法set
方法(这是WP_Query
class) 的作用是传递 2 个参数——一个查询参数键和一个查询参数值。所以你的第一个例子(代码A)是正确的方法。
您可以在这里查看方法代码:https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/query.php#L2328 https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/query.php#L2328
附:
如果您想从查询中排除多个类别,请考虑使用category__not_in
查询参数,例如:
$query->set( 'category__not_in', array(60, 61) );
有关类别查询参数的更多信息,请查看http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)