问题描述

我试图拉多个网站的帖子。例如,我可以通过一个类别和总帖子拉出一个单一的站点帖子 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 the switch_to_blog() process, $blog_id will 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。