我正在使用WordPress,在搜索页面上,我需要获取该类别中的帖子数量并将其显示在名称旁边。像这样
Cat 1(3) 猫2(1) 猫3(18) ....
目前我正在使用get_categories()
函数,这些函数会为您提供一个对象并使用$cat->count()
获取计数。这会抓住术语中的帖子总数,但我只需要从当前查询中的帖子中获取该计数。我正在使用pre_get_posts
来更新查询。有人知道这样做的方法吗?
这是我当前的代码块
foreach ( get_categories() as $cat ) {
if ( $cat->parent === 0 ) {
$checked = ( in_array( $cat->slug, $check ) ) ? 'checked' : '';
printf( '<input id="%1$s" %4$s type="checkbox" name="category[%1$s]"><label for="%1$s" >%2$s ( %3$d )</label><br>',
$cat->slug,
$cat->name,
$cat->count,
$checked
);
}
}
以下是我的pre_get_posts
操作:
add_action( 'pre_get_posts', 'breed_search_query' );
function breed_search_query( $query ) {
$cats = ( isset( $_GET['category'] ) ) ? implode( ',', array_keys( $_GET['category'] ) ) : null;
$search = ( isset( $_GET['s'] ) ) ? sanitize_text_field( $_GET['s'] ) : null;
if ( ! is_admin() && $query->is_main_query() && is_search() ) {
$query->set( 'post_type', 'post' );
$query->set( 'posts_per_page', 8 );
$query->set( 's', $search );
$query->set( 'category_name', $cats );
}
}
答案 0 :(得分:6)
必须说好的和有趣的问题。不幸的是,你不会让它与get_categories
一起使用,所以你需要另一种方法。
以下是我们将如何做到这一点:
创建非常精简的自定义查询,以便从与查询匹配的所有帖子中获取所有帖子ID
获取附加到帖子的所有类别
遍历帖子中的所有帖子和类别,并构建一个数组,其中类别名称为键,帖子ID为值
循环显示此新数组,并显示类别名称,并计算并显示该特定键(类别)的帖子数量
( 注意: 由于新的数组语法([]
),以下代码未经测试且需要使用PHP 5.4+。)
$cats = ( isset( $_GET['category'] ) ) ? implode( ',', array_keys( $_GET['category'] ) ) : null;
$search = ( isset( $_GET['s'] ) ) ? sanitize_text_field( $_GET['s'] ) : null;
$args = [
'post_type' => 'post',
'nopaging' => true, // Gets all posts
's' => $search,
'category_name' => $cats,
'fields' => 'ids', // Makes the query extremely lean, excellent for performance, only get post ID's
];
$q = get_posts( $args );
// Define our $category_array which will hold the category name/post ID's array
$category_array = [];
// Define our $category_info array which will hold the category name as key and the object as value
$category_info = [];
foreach ( $q as $post ) {
// Get the categories attached to a post
$categories = get_the_category( $post );
foreach ( $categories as $category ) {
// Create our array which will hold the category name as key and post ID's as values
$category_array[$category->name][] = $post;
// Create an array to hold the category objects. Use names as keys and objects as values
$category_info[$category->name] = $category;
} // endforeach $categories
} // endforeach $wp_query->posts
wp_reset_postdata();
// Sort the array keys alphabetical. You can change or remove this as necessary
ksort( $category_array );
foreach ( $category_array as $k=>$v ) {
$category_slug = $category_info[$k]->slug;
$category_parent = $category_info[$k]->parent;
$checked = ( in_array( $category_slug, $check ) ) ? 'checked' : ''; // Just make sure that $check is defined somewhere
// Build our string
printf ( '<input id="%1$s" %4$s type="checkbox" name="category[%1$s]"><label for="%1$s" >%2$s ( %3$d )</label><br>', // I have not modified anything in this line
$category_slug, // Holds the category slug
$k, // Holds the category name
count( $v ), // Counts and return the post count
$checked
);
} // endforeach $category_array