从多个类别随机显示查询帖子

时间:2017-05-30 21:22:08

标签: php wordpress while-loop

我希望我的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>

2 个答案:

答案 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'
相关问题