我正在做一系列使用的循环:
$myposts = get_posts("category=$category->id");
获取给定类别的所有帖子。但是我需要做一些事情:
$myposts .= get_posts("category=$category->id");
所以每次循环都会添加var $ myposts。然后我需要按发布日期重新订购$ myposts并重新循环。
知道如何实现这个目标吗?
我的完整代码是:
global $wpdb;
$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description
from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '2' ORDER BY name ASC");
echo '<ul class="results">';
foreach($categories as $category) :
?>
<?php global $post;
$myposts = get_posts("category=$category->id");
foreach($myposts as $post) : setup_postdata($post);
?>
<li>
<a class="title" href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span><br/><?php echo $category->name; ?></a>
<a class="image" href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post->ID, 'result') ?></a>
</li>
<?php endforeach; ?>
<?php endforeach;
echo '</ul>';
谢谢,
戴夫
答案 0 :(得分:2)
在最简单的层面上,你不能在数组上连接(在字符串上使用点运算符)。尝试使用array_merge
$myposts = get_posts("category=$category->id");
$myposts = array_merge($myposts, get_posts("category=$category->id"));
虽然,我认为你想要实现的是几个类别的帖子列表正确吗?上面的例子将给出可能的重复(如果相同的帖子在多个类别中)尝试更多的WP方式:
//array of categories
$categories = array();
// I assume you are looping through something to get the multiple `$category->id`
for($some_category_objs as $category)
{
$categories[] = $category->id;
}
// query for posts where the post has every category in the list
$myposts = query_posts(array('category__and' => $categories));
// query for posts where the post matches any of the categories
$myposts = query_posts('cat='.implode(",", $categories));
你的帖子对你的目标有点模糊。如果我不在基地,请告诉我。另外,请查看:http://codex.wordpress.org/Template_Tags/query_posts
$args = array(
'category__in' => $categories, // $categories is an array of all the categories to search in
'orderby' => 'date', // ORDER BY DATE
'order' => 'DESC', // NEWWST FIRST
);
$my_posts = get_posts($args);