我希望我的WP_Query能够随机显示阵列中列出的所有类别中的六个帖子。
我编写的循环/查询仅显示数组中第一个类别(Art)的帖子。
我有什么问题?
<div class="main-news">
<!-- Define our WP Query Parameters -->
<?php $the_query6 = new WP_Query(array('posts_per_page' => 6, 'category_name' => 'Art' , 'Technology', 'Fashion-Beauty')); ?>
<!-- Start our WP Query -->
<?php while ($the_query6 -> have_posts()) {
$the_query6 -> the_post(); ?>
<div class="new-content">
<!-- Display the Post Image with Hyperlink -->
<div class="new-image"><?php the_post_thumbnail('fashion');?></div>
<div class="new-content-excerpt">
<!-- Display the Post Category Hyperlink -->
<h5><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></h5>
<!-- Display the Post Title with Hyperlink -->
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
</div>
</div>
<!-- Repeat the process and reset once it hits the limit -->
<?php } ?>
<?php wp_reset_postdata(); ?>
</div>
答案 0 :(得分:0)
将category
放在category IDs
数组中似乎可以解决问题。
我使用了以下代码:
<?php $the_query6 = new WP_Query(array('category__in' => array( 240, 243, 239, ),'posts_per_page' => 4)); ?>
答案 1 :(得分:0)
罪魁祸首是:
<?php $the_query6 = new WP_Query(array(
'posts_per_page' => 6,
'category_name' => 'Art',
'Technology',
'Fashion-Beauty')
); ?>
要提取属于不同类别的帖子,必须使用以下格式(read more about it on WordPress' documentation):
'category_name' => 'art,technology,fashion-beauty'
'category_name' => 'art+technology+fashion-beauty'