0

我正在做一系列循环,这些循环使用:

$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>';

谢谢,

戴夫

4

1 回答 1

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);
于 2013-07-15T14:30:03.947 回答