問題描述
我試圖拉多個網站的帖子。例如,我可以通過一個類別和總帖子拉出一個單一的站點帖子 10 。
但是我試圖從兩個獨立的多站點博客 1& 但只有博客 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%20echo%20get_page_link($post->ID);%20?>" title="<?php echo $post->post_title; ?>"><?php echo short_title('...', 7); ?></a>
</li>
<?php endforeach ; ?>
<?php restore_current_blog(); //switched back to main site ?>
最佳解決方案
WordPress 函數 switch_to_blog()期望一個整數作為輸入參數。您可以在食典中閲讀更多信息:
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 名為巴赫:
PS:感謝 @brasofilo 提供 link,澄清了我對 restore_current_blog()的誤解;-)
PPS:感謝 @ChristineCooper 分享以下評論:
Just a friendly warning. Make sure to not set your original blog id to variable
$blog_id— this is because during theswitch_to_blog()process,$blog_idwill be overriden by the core function, meaning what when you try to switch back to the original blog, you will end up with switching to the last one you looped through. A little bit of a mind-puzzle. 🙂
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
