【发布时间】:2017-05-31 17:39:51
【问题描述】:
我希望我的 WP_Query 随机显示来自数组中列出的所有类别的总共六个帖子。
我编写的循环/查询仅显示数组中第一个类别(艺术)的帖子。
我做错了什么?
<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>
【问题讨论】:
-
你试过用
'category' => id_of_the_category替换'category_name' => 'Art' , 'Technology', 'Fashion-Beauty')(无论如何都是无效的)吗? -
WP Codex 似乎认为 category_name 没问题。但我将尝试使用类别 ID。谢谢:)
-
它应该是一个类别 slug 的数组(
'category_name' => array('art', 'technology', 'fashion-beauty'),你没有创建一个。
标签: php wordpress while-loop