【发布时间】:2014-05-21 04:23:39
【问题描述】:
我正在运行一个多站点,我想从两个博客中提取某些类别的帖子。因此,我正在为每个博客运行一个循环来提取帖子,因为其中一个类别在博客 1 中,而另一个类别在博客 2 中。
<?php $value = array(); ?>
<?php
// Get the values from $_POST
$original_blog_id = get_current_blog_id(); // get current blog
$bids = array(1,2); // all the blog_id's to loop through EDIT
foreach($bids as $bid):
switch_to_blog($bid);
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'Music',
'field' => 'slug',
'terms' => array('artist', 'club')
),
)
);
$the_query = new WP_Query( $args );
?>
<?php $postids = array(); ?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $postids[]=get_the_ID(); ?>
<?php endwhile; ?>
<?php $value[] = $postids; ?>
<?php
} else {
// no posts found
echo 'Nothing found.';
}
?>
<?php endforeach; ?>
<?php
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($value));
$list = iterator_to_array($it,false);
$posts = new WP_Query(array(
'post__in' => $list,
'post_type' => 'post',
));
?>
<?php
foreach ($posts as $post) {
setup_postdata($post);
?>
<?php echo the_title(); ?>
<?php
}
wp_reset_postdata(); ?>
<?php switch_to_blog($original_blog_id); ?>
我在数组中获取IDs 的原因:
<?php $postids[]=get_the_ID(); ?>
因为我想获取随机帖子。如果此时我得到帖子的标题和内容而不是上面的语句,那么它将按顺序显示。像这样的:
BLOG1: POST1,POST2, POST : BLOG2: POST1, POST2, POST3
但我希望帖子像这样随机排列:
BLOG1: POST1, BLOG2: POST3, BLOG1: POST2, BLOG2: POST1: BLOG2: POST2
所以一切正常,即使在 foreach 循环之外,我也可以得到 posts IDs,但问题是:
我无法从IDs 那里获取帖子内容。它只给我来自博客 2 的帖子,因为当前博客是 2。但即使 postID 在 list 数组中,它也没有显示来自 blog1 的任何内容。
有人可以帮忙吗?
【问题讨论】:
标签: php foreach while-loop wordpress