【发布时间】:2014-10-20 03:12:01
【问题描述】:
我正在查看我的 functions.php 并想知道为什么 CODE A 使用 add_action 而 CODE B 使用 add_filter ?
CODE A 的主要目标是包含和排除特定类别。
CODE B 的主要目标是排除特定类别。
将add_action用于CODE A是否正确
和add_filter 代码B?
代码 A:显示主页的特定类别(称为“精选”),而不是“最新帖子”
function featured_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'featured' );
$query->set( 'category__not_in', array(60, 61) );
$query->set( 'posts_per_page', 5 );
}
}
add_action( 'pre_get_posts', 'featured_category' );
代码 B:在搜索结果中排除“赞助帖子类别”
function search_filter($query) {
if ( $query->is_search && $query->is_main_query() ) {
$query->set('post_type', 'post');
$query->set( 'category__not_in', array(60, 61) );
$query->set( 'posts_per_page', 20 );
}
return $query;
}
add_filter('pre_get_posts', 'search_filter');
【问题讨论】:
标签: php wordpress add-filter