,而帖子总数为10。但是只有博客1有效。另外,我想从博客1和博客2中提取另一个类别。我该怎么办?
这是我要尝试的操作:
<?php
global $switched;
switch_to_blog(1,2); //switched to 1 & 2 but only 1 working
// Get latest Post
$latest_posts = get_posts('&cat=64&showposts=10');
$cnt =0;?>
<ul>
<?php foreach($latest_posts as $post) : setup_postdata($post);?>
<li>
<a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo $post->post_title; ?>"><?php echo short_title('...', 7); ?></a>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>
#1 楼
WordPress函数switch_to_blog()
需要一个整数作为输入参数。您可以在Codex中阅读有关它的更多信息:http://codex.wordpress.org/Function_Reference/switch_to_blog
请尝试以下这种结构:
// Get the current blog id
$original_blog_id = get_current_blog_id();
// All the blog_id's to loop through
$bids = array( 1, 2 );
foreach( $bids as $bid )
{
// Switch to the blog with the blog_id $bid
switch_to_blog( $bid );
// ... your code for each blog ...
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
更新:
如果要为每个博客获取不同类别的帖子,则可以使用例如:
// Get current blog
$original_blog_id = get_current_blog_id();
// Setup a category slug for each blog id, you want to loop through - EDIT
$catslug_per_blog_id = array(
1 => 'video',
4 => 'news'
);
foreach( $catslug_per_blog_id as $bid => $catslug )
{
// Switch to the blog with the blog id $bid
switch_to_blog( $bid );
// ... your code for each blog ...
$myposts = get_posts(
array(
'category_name' => $catslug,
'posts_per_page' => 10,
)
);
// ... etc
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
示例:
这里是一个允许您使用模板标签的示例(这适用于我的多站点安装):
// Get current blog
$original_blog_id = get_current_blog_id();
// Setup a category for each blog id you want to loop through - EDIT
$catslug_per_blog_id = array(
1 => 'video',
4 => 'news'
);
foreach( $catslug_per_blog_id as $bid => $catslug )
{
//Switch to the blog with the blog id $bid
switch_to_blog( $bid );
// Get posts for each blog
$myposts = get_posts(
array(
'category_name' => $catslug,
'posts_per_page' => 2,
)
);
// Skip a blog if no posts are found
if( empty( $myposts ) )
continue;
// Loop for each blog
$li = '';
global $post;
foreach( $myposts as $post )
{
setup_postdata( $post );
$li .= the_title(
$before = sprintf( '<li><a href="%s">', esc_url( get_permalink() ) ),
$after = '</a></li>',
$echo = false
);
}
// Print for each blog
printf(
'<h2>%s (%s)</h2><ul>%s</ul>',
esc_html( get_bloginfo( 'name' ) ),
esc_html( $catslug ),
$li
);
}
// Switch back to the current blog
switch_to_blog( $original_blog_id );
wp_reset_postdata();
这是上面示例的演示屏幕截图,其中站点1名为贝多芬,站点4名为Bach:
PS:感谢@brasofilo提供该链接澄清了我对
restore_current_blog()
的误解;-) PPS:感谢@ChristineCooper分享了以下评论:
只是一个友好的警告。确保不要将原始博客ID设置为
变量
$blog_id
-这是因为在switch_to_blog()
进程中,
$blog_id
将被核心功能覆盖,这意味着当您尝试切换时回到原始博客,您最终将切换到循环浏览的最后一个博客。有点令人费解。 :)
#2 楼
看看我的“ Multisite Post Reader”插件https://wordpress.org/plugins/multisite-post-reader/中的代码。它在另一个答案中使用该技术遍历帖子。我也有用于图像处理相同功能的插件。由于它是开放源代码,因此欢迎您在代码中四处逛逛,并将其用于自己的用途。 (其中一些代码是从我发现的开源代码中修改的。)
评论
这是我如何加载我的帖子pastie.org/7827649如何将其实现为这点,就像我通过两个博客ID提到的那样,但按特定类别博客1列出的每个博客ID都会具有类别视频,而博客2也会具有类别新闻总数不得超过10个。
– DeadArtcore
13年5月10日在17:20
我更新了答案以支持不同的类别。
–birgire
13年5月10日在18:39
ps:再次更新了示例,因此您可以使用模板标签,例如the_title()代替$ post-> post_title ;-)我希望我给了您足够的信息,以便您可以完成项目。
–birgire
13年5月11日在19:08
用粘贴中的foreach(将$ posts作为$ post)替换为foreach($ rightbox作为$ post)。
–birgire
13年5月11日在22:27
希望我可以将您的评论添加到更新的答案@ChristineCooper中
–birgire
16年4月2日在22:48