WordPress

Pobierz najnowsze posty z wielu witryn

  • 24 marca, 2020
  • 3 min read
Pobierz najnowsze posty z wielu witryn


Stworzyłem multisite na lokalnej maszynie wirtualnej i zainstalowałem mój motyw.

Teraz próbuję pobrać najnowsze posty ze wszystkich witryn, aby wyświetlić je uporządkowane według ostatniej aktualizacji na stronie głównej.

Z tego, co do tej pory przeczytałem, najlepszą opcją powinno być utworzenie niestandardowego szablonu dla mojej strony głównej. Czy to jest poprawne? Więc biorę kod w moim szablonie strona.php ale nie mogę zrozumieć jak to zmienić.

    <?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=" 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();

EDYTOWAĆ
Udało mi się to osiągnąć za pomocą tego kodu:

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 );
    }


Źródło

Warto przeczytać!  Ukryj przycisk „Załaduj więcej”, jeśli w wybranej kategorii nie ma już więcej postów