skip to Main Content

I am working on a site with a page that has multiple sections, each section has multiple loops featuring multiple categories. I use Ajax Load More plugin to load new posts for each sections. The issue is when I click on Load More, it loads both the posts already shown and the one that hasn’t been shown. I want it to load only new posts not already shown.

Here is the shortcode I used:

echo do_shortcode('[ajax_load_more container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');

Here is the loop on on of the sections

<div class="row">

                <div class="col-lg-12 col-sm-12">
                    <div class="music_box bg-color1">
                        <div class="music_box_top">

                            <?php
                            $sticky = get_option( 'sticky_posts' );
                            rsort( $sticky );

                            $args = array(
                                'post__in' => $sticky,
                                'posts_per_page' => 1,
                                'cat' => 34
                                );

                            $sticky_query = new WP_Query( $args );

                            while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); 
                            ?>
                            <a href="<?php the_permalink(); ?>">
                                <div class="fashion_box_thumb">
                                    <?php
                                    if ( has_post_thumbnail() ) {
                                        the_post_thumbnail( 'full', array() );
                                    }
                                    ?>
                                </div>
                            </a>

                            <div class="fashion_box_text">
                                <a href="<?php the_permalink(); ?>">
                                    <h3><?php the_title(); ?></h3>
                                </a>
                                <p><?php the_excerpt(); ?></p>

                                <div class="post_cont_icons">
                                    <span class="fa fa-comments cmnt"> <?php comments_number('0','1','%'); ?></span>
                                    &nbsp; &nbsp;           
                                    <?php echo getPostLikeLink(get_the_ID());?>

                                    <span class="matchtime2"><i class="fa fa-clock-o"></i> <?php the_time();?><br></span>
                                </div>

                            </div>


                            <?php endwhile; ?>
                            <?php wp_reset_postdata(); ?>

                            <div class="clear"></div>

                        </div><!--music_box_top-->

                        <div class="fashion_box_bottom">

                            <?php
                            $args = array(
                                'post__not_in' => get_option( 'sticky_posts' ),
                                'posts_per_page' => 4,
                                'cat' => 34
                                );

                            $sticky_query = new WP_Query( $args );

                            $count = 0;

                            while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
                                <?php $count++; ?>
                                <?php if ($count == 1) : 
                            ?>

                            <div class="fashion_box_bottom_item">
                                <a href="<?php the_permalink(); ?>">
                                    <h4><?php the_title(); ?></h4>
                                </a>
                            </div>

                            <?php elseif ($count == 2) : ?>
                            <div class="fashion_box_bottom_item">
                                <a href="<?php the_permalink(); ?>">
                                    <h4><?php the_title(); ?></h4>
                                </a>
                            </div>

                            <?php elseif ($count == 3) : ?>
                            <div class="fashion_box_bottom_item">
                                <a href="<?php the_permalink(); ?>">
                                    <h4><?php the_title(); ?></h4>
                                </a>
                            </div>

                            <?php elseif ($count == 4) : ?>
                            <div class="fashion_box_bottom_item">
                                <a href="<?php the_permalink(); ?>">
                                    <h4><?php the_title(); ?></h4>
                                </a>
                            </div>                        

                            <div class="clear"></div>

                        </div><!--music_box_bottom-->
                    </div><!--music_box-->
                </div><!--col-lg-12-->

                <?php else :
                get_template_part( 'woodclefpro/pro_template3' );

                endif;
                endwhile;
                wp_reset_postdata(); ?>

            </div><!--row-->
            <div class="row">
            <?php 

                echo do_shortcode('[ajax_load_more container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
            ?>
            </div>

2

Answers


  1. Chosen as BEST ANSWER

    This is for those that might come across the question above and are facing the same issue that I was. Here is how I solved it.

    Add the code below right before endwhile

    $do_not_duplicate[] = $post->ID;
    

    Add this inside your shortcode: post__not_in="'.$post__not_in.'"

    Then your final shortcode looks like this:

    echo do_shortcode('[ajax_load_more ajax_load_more post__not_in="'.$post__not_in.'" container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
    

  2. Not quite right. The fact is that on the page of one of the Addons to this plugin say that the template output single entry, for example “single.php” should be nothing but a shortcode. All content single.php should be placed in the template used by the plugin. Sorry for the crooked English.
    https://connekthq.com/plugins/ajax-load-more/add-ons/single-posts/

    Note: Ajax Load More will take care of loading ALL posts, including
    the initial post when a user lands on the page. All that should remain
    in your single.php loop is the ajax_load_more shortcode (as seen
    above).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search