这是我一直在使用的代码,为了简洁起见,排除了前面的查询参数。如您所见,这来自一个显示帖子网格的小部件。问题是,我不知道如何编写一个查询或多个查询,以有效的方式从六个类别中的每一个类别中检索最新的帖子。
$r = new WP_Query( apply_filters( \'widget_posts_grid\', $query ) );
if ( $r->have_posts() ) :
?>
<?php echo wp_kses_post( $args[\'before_widget\'] ); ?>
<?php
if ( $title ) {
echo wp_kses_post( $args[\'before_title\'] . $title . $args[\'after_title\'] );
}
?>
<div class="posts-grid">
<div class="row">
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<div class="col-md-4 col-sm-6">
<?php get_template_part( \'content\', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
</div>
<?php echo wp_kses_post( $args[\'after_widget\'] ); ?>
<?php
wp_reset_postdata();
endif;
我尝试了使用array\\u merge的foreach循环,但结果显然无法循环通过。。。
$do_not_duplicate = array();
$r = new WP_Query();
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
\'cat\' => $category->term_id,
\'post_type\' => \'post\',
\'posts_per_page\' => \'1\',
\'post__not_in\' => $do_not_duplicate
);
$little_array = new WP_Query( $args );
$r = array_merge( $r, $little_array->posts );
while ( $r->have_posts() ) { $r->the_post();
$do_not_duplicate[] = $post->ID;
}
}
$r->post_count = count( $r->posts );
for($i = 1; $r->have_posts(); $i++) {
$r->the_post();
}
if ( $r->have_posts() ) :
根据我的研究,我真的认为这会奏效。然而,似乎在
$r = array_merge( $r, $little_array->posts );
,
$r
空出来了。
print_r($r);
不打印任何内容。
我感谢你的帮助。
以下是上述查询后的代码,以供参考:
if ( $r->have_posts() ) :
?>
<?php echo wp_kses_post( $args[\'before_widget\'] ); ?>
<?php
if ( $title ) {
echo wp_kses_post( $args[\'before_title\'] . $title . $args[\'after_title\'] );
}
?>
<div class="posts-grid">
<div class="row">
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<div class="col-md-4 col-sm-6">
<?php get_template_part( \'content\', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
</div>
<?php echo wp_kses_post( $args[\'after_widget\'] ); ?>
<?php
wp_reset_postdata();
endif;
最合适的回答,由SO网友:Pieter Goosen 整理而成
我真的很难在这里运行几个查询,每个类别一个查询。我们需要在这里保持聪明,以避免许多不必要的工作。
让我们看看代码;(我将对其进行评论,以简化理解过程)
// Get the categories. We will only get the category ID\'s to speed things up
$category_args = [
\'fields\' => \'ids\',
];
$categories = get_categories ( $category_args );
// Check if we have categories to avoid bugs
if ( $categories ) {
// Define the variable to hold an array of posts not to duplicate
$do_not_duplicate = [];
// Define a variable to hold our posts
$posts_array = [];
foreach ( $categories as $cat_id ) {
// Setup our query arguments to get our posts
$args = [
\'cat\' => $cat_id,
\'posts_per_page\' => 1,
\'post__not_in\' => $do_not_duplicate,
\'fields\' => \'ids\' // Only get post id\'s to increase performance
];
/**
* Lets use get_posts as we do not need the whole object and get_posts by default
* legally breaks pagination which makes the query faster, and it automatically
* ignore sticky posts and by default does not get modified by filters
*/
$q = get_posts( $args );
// Check if we have posts
if ( $q ) {
/**
* Now we need to add the post ID to the $do_not_duplicate array.
* We will also pass the posts in $q to $posts_array
* NOTE: you will need to rework this if you ever need more than one post per category
*/
$do_not_duplicate[] = $q[0];
$posts_array[] = $q[0];
}
} //endforeach $categories
// We can now run an instance of WP_Query to get the posts and query object
if ( $posts_array ) {
$final_args = [
\'posts_per_page\' => count( $posts_array ),
\'post__in\' => $posts_array,
\'ignore_sticky_posts\' => 1, // Ignore stickies
\'no_found_rows\' => true, // Skip pagination, remove if needed
];
$final_query = new WP_Query( $final_args );
var_dump( $final_query );
// Now we can run te loop and output our posts
if ( $final_query->have_posts() ) {
while ( $final_query->have_posts() ) {
$final_query->the_post();
the_title();
} //endwhile
wp_reset_postdata(); // NEVER EVER forget this line
} // endif $final_query->have_posts()
} // endif $posts_array
} // endif $categories
您可以调整、扩展和修改它以满足您的需要。注意,代码中的所有内容都已设置为每个类别只能容纳一篇文章。您需要改变方式
$do_not_duplicate
和
$posts_array
如果您要查询每个类别的多个帖子,则生成