0

I am using Multisite and have the below code which pulls in posts from 2 sites.

How can I add Ajax "Load More" pagination to the below code? Ideally when a "Load more" button is clicked it displays the next set of posts on the page.

$allPosts = [];
$sites = array( 2, 1 );
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

foreach( $sites as $site ){
    switch_to_blog( $site );

    $allBlogPosts = get_posts( array(
        'category_name' => 'direct, uncategorized',
        'post_status' => 'publish',
        'orderby' => 'publish_date',
        'order' => 'DESC',
        'posts_per_page' => 2,
        'paged' => $paged,
    ));

    foreach( $allBlogPosts as $post ){
        ob_start();
        setup_postdata( $post ); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="post__header">
                <?php the_post_thumbnail(); ?>
                <?php if(!is_single()) { ?>
                    <time class="timeStamp"><span class="timeStamp__month"><?php the_time("d"); ?> </span> <span class="timeStamp__day"> <?php the_time("F"); ?> </span> <span class="timeStamp__year"> <?php the_time("Y"); ?> </span> </time>
                <?php } ?>
                <?php
                if ( is_single() ) {
                    the_title( '<h1 class="post__title">', '</h1>' );
                } else {
                    the_title( '<h2 class="post__title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
                }

                if ( 'post' === get_post_type() && is_single() ) : ?>
                    <div class="post__meta">
                        <?php themway_posted_on(); ?>
                    </div><!-- .post__meta -->
                <?php
                endif; ?>
            </header><!-- .post__header -->


        </article><!-- post -->
        <?php
        $allPosts[] = ['display'=>ob_get_clean(), 'post_date'=>get_the_time('U')];
        wp_reset_postdata();
    }// end foreach for $skiPost as $post

    restore_current_blog();
}// end foreach for $sites as $site

function sortPosts($a, $b){
    return $b['post_date'] - $a['post_date'];
}
usort($allPosts, 'sortPosts');
foreach($allPosts as $p) echo $p['display'];

?>
  • Can anyone help? – ianhman May 11 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.