从多站点获取最新帖子

时间:2020-03-24 作者:CrazYoshi

I\'ve created a multisite on a local VM and installed my theme.

Now I\'m trying to get the latest posts from all the sites in order to display them ordered by last updated on homepage.

From what I\'ve read so far, the best option should be creating a custom template for my home page. Is it correct? So I take the code in my template page.php but I cannot understand how to change it.

    <?php
    /**
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages and that other
     * \'pages\' on your WordPress site will use a different template.
     *
     */

    get_header();

    global $heap_private_post;

    if ( post_password_required() && ! $heap_private_post[\'allowed\'] ) {
        // password protection
        get_template_part( \'theme-partials/password-request-form\' );

    } else { ?>
        <div class="page-content single-page-content">

            <?php if ( have_posts() ): the_post(); ?>

                <article class="article page page-single page-regular">
                    <header>
                        <?php if ( has_post_thumbnail() ):
                            $image = wp_get_attachment_image_src( get_post_thumbnail_id(), \'full-size\' );
                            if ( ! empty( $image[0] ) ): ?>
                                <div class="page__featured-image">
                                    <img src="<?php echo $image[0] ?>" alt="<?php the_title(); ?>"/>
                                </div>
                            <?php endif;
                        endif; ?>
                    </header>
                    <div class="page__wrapper">
                        <section class="page__content  js-post-gallery  cf">
                            <h1 class="page__title"><?php the_title(); ?></h1>
                            <hr class="separator separator--dark"/>
                            <?php the_content(); ?>

                        </section>
                        <?php
                        global $numpages;
                        if ( $numpages > 1 ):
                            ?>
                            <div class="entry__meta-box  meta-box--pagination">
                                <span class="meta-box__title"><?php _e( \'Pages\', \'heap\' ) ?></span>
                                <?php
                                $args = array(
                                    \'before\'           => \'<ol class="nav  pagination--single">\',
                                    \'after\'            => \'</ol>\',
                                    \'next_or_number\'   => \'next_and_number\',
                                    \'previouspagelink\' => __( \'&laquo;\', \'heap\' ),
                                    \'nextpagelink\'     => __( \'&raquo;\', \'heap\' )
                                );
                                wp_link_pages( $args );
                                ?>
                            </div>
                            <?php
                        endif;

                        //comments
                        if ( comments_open() || \'0\' != get_comments_number() ):
                            comments_template();
                        endif; ?>
                    </div>
                </article>

                <?php
            else :
                get_template_part( \'no-results\' );
            endif; ?>

        </div><!-- .page-content -->

    <?php } // close if password protection

    get_footer();

EDIT I was able to achieve it using this code:

foreach( $subsites as $subsite ) {
    $subsite_id = get_object_vars($subsite)["blog_id"];
    $subsite_name = get_blog_details($subsite_id)->blogname;
    switch_to_blog($subsite_id);
    $blog_posts = get_posts(); 
    restore_current_blog();
    foreach( $blog_posts as $post ) {
        setup_postdata( $post );
    }
1 个回复
SO网友:Rick Hellewell

实际上,有一种方法可以从所有多站点子站点获取帖子。我在这里用我的“Multisite Media Display”插件来实现https://wordpress.org/plugins/multisite-media-display/ .

我使用它来显示我的多站点中的所有媒体项目,以确保任何提交都符合站点要求。效果很好。

我有一个类似的帖子插件,但它不适用于当前的WP版本。更新它的优先级较低。

但您可以获得所有子站点的数组(尽管这样做的功能不适用于4.6之前的WP版本)。然后循环遍历该数组,根据需要执行post查询。

ADDED

下面是一些代码,带有注释。代码将获取所有子网站,然后切换到每个子网站,您可以在其中输入查询并在“循环”中输出帖子。

$subsites_object = get_sites(); // get the sites into an object
// convert to an array
$subsites = objectToArray($subsites_object);
// process each subsite
foreach ($subsites as $subsite) {
// get some values for later if needed
    // $subsite_id is important; that switches to the subsite
    $subsite_id     = $subsite["blog_id"];
    $subsite_name   = get_blog_details($subsite_id)->blogname;
    $subsite_path   = $subsite["path"];
    $subsite_domain = $subsite["domain"];
    switch_to_blog($subsite_id);
// do your post query, then do \'the loop\' to get posts
}
您可以将其放入模板中。或者,您可以为输出内容的短代码添加代码。

谷歌/bings/ducks上有很多帮助。查看WP代码文档,了解每件事的作用。

相关推荐

MultiSite有SSL证书,但媒体库仍为http://

我们的多站点WordPress安装的URL为https://xx.com 对于主域,例如,https://yy.xx.com 作为子网站。我们正在使用WP的内置域映射功能。媒体库链接仍显示为http://xx 尽管如此,子域上使用的任何图像都被标记为不安全。只有一个子域(我们最近的一个子域)具有与结构的媒体库链接https://zz.xx.com. SSL证书可能是在创建其他子域之后安装的,但在ZZ之前:我不确定。我如何获得WP媒体库链接,所有链接都显示为https://-最好是URL中的子域?理想情况下