• So I have tried to tackle this a couple of times and am just not having any luck. So right now I have archive pages set up so that when a user first visits the page, it will display the first three posts belonging to that archive. As they scroll down infinite scroll is supposed to kick in and then load 3 additional posts with each load. I also have a filter on the page so that users can filter posts by custom taxonomies, and then new posts load on the page using the same infinite scroll functionality.

    So the problem is that although infinite scroll seems to be working fine for the filtered results, it is giving me problems on the initial page load. Initially the page shows the first three posts, however when scrolling down infinite scroll kicks in and then repeats the same first three posts. You can see and example here: http://projects.server291.com/nymj/category/clinical-article/

    I am assuming there is something wrong with my code. Here it is relevant code. The only difference I can think of is that infinite scroll isn’t playing nice with the default query on the archive page. Just a though since it is working fine with the filter which is utilizing a custom WP_Query. Also, this is being used for a custom post type.

    Infinite Scroll Functions

    /**
     * Infinite scroll rendering
     */
    function nymj_infinite_scroll() {
    
      // On issues archive
      // if( is_page_template( 'issues' ) ) {
    
      // }
    
      // Default archives
      if( is_archive() ) {
    
        while( have_posts() ) : the_post();
          include( locate_template( 'templates/article-snippet.php' ) );
        endwhile;
       }
    
    }
    
    /**
     * Infinite scroll functionality
     */
    add_theme_support( 'infinite-scroll', array(
        'type'           => 'scroll',
        'footer_widgets' => false,
        'container'      => 'infinite-scroll',
        'wrapper'        => false,
        'footer'         => false,
        'render'         => 'nymj_infinite_scroll',
        'posts_per_page' => 3
    ) );

    Initial Archive Loop

    <div id="infinite-scroll" class="article-box__body has-custom-scrollbar">
    
      <?php
        if ( have_posts() ) :
          while ( have_posts() ) : the_post();
            include( locate_template( 'templates/article-snippet.php' ) );
          endwhile;
    
        endif;
      ?>
    
    </div>

    https://wordpress.org/plugins/jetpack/

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    The results will depend on the contents of templates/article-snippet.php. I would recommend that you make sure you don’t have anything that could customize the query in there.

    I would also recommend against using the infinite-scroll ID, though, to avoid conflicts with other containers using that name. It might be best to stick with something like archive-content, for example.

    Let me know if that helps!

    Thread Starter Kevin

    (@truheart)

    Thanks, I really appreciate the quick response. Unfortunately neither of these seem to be causing the issue. Just in case I changed the ID, and article-snippet doesn’t really contain anything that would effect the query. Just in case here is the code (some custom functions but none that I would think would cause issues here:

    $fields = nymj_get_fields([
    	'pdf',
    	'localize--summary'
    ]);
    $cat = get_the_terms( get_the_id(), 'category' );
    $cat_name = sprintf( __( '%s', 'nymj' ), $cat[0]->name );
    $title = get_the_title();
    $authors = nymj_get_authors();
    
    $issue = get_the_terms( $current_id, 'issue' );
    $issue_date = get_field( 'meta_issue_published_date', 'issue_' . $issue[0]->term_id );
    $issue_date = sprintf( __( '%s', 'nymj' ), $issue_date );
    
    $url = get_post_permalink();
    $summary = $fields['summary'];
    if( !$summary ) {
    	$summary = nymj_make_summary( 150 );
    }
    ?>
    
    <!-- SINGLE SNIPPET -->
    <article class="article-snippet" itemscope itemtype="http://schema.org/MedicalScholarlyArticle">
    
    	<!-- Tag -->
    	<div class="article-snippet__tag" itemprop="articleSection"><?php echo $cat_name; ?></div>
    
    	<div class="article-snippet__content-wrap">
    
    		<!-- Title -->
    		<h3 class="article-snippet__title" itemprop="headline">
    			<span class="main"><?php echo $title; ?></span>
    		</h3>
    
    		<!-- Summary -->
    		<p class="article-snippet__summary" itemprop="description">
    			<?php echo $summary; ?>
    		</p>
    
    		<!-- Meta -->
    		<div class="article-snippet__meta">
    
    			<div class="authors">
    				<i><?php _e( 'by', 'nymj' ); ?></i>
    				<ul>
    					<li itemprop="author"><?php echo $authors[0]; ?></li>
    				</ul>
    			</div>
    
    			<div class="published" itemprop="datePublished">
    				<?php echo $issue_date; ?>
    			</div>
    
    			<div class="links">
    				<ul class="inline-list">
    					<li><?php if( function_exists( 'sharing_display' ) ) { sharing_display( '', true ); } ?></span></li>
    					<?php if( $fields['pdf'] ) { ?>
    						<li><a href="<?php echo $fields['pdf']['url']; ?>" title="<?php _e( 'Download the PDF', 'nymj' ); ?>" target="_blank"><i class="icon-pdf"></i><?php _e( 'PDF', 'nymj' ); ?></a></li>
    					<?php } ?>
    					<li><a href="<?php echo $url; ?>" itemprop="url"><?php _e( 'Read Full Text', 'nymj' ); ?></a></li>
    				</ul>
    			</div>
    
    		</div>
    
    	</div>
    
    </article>
    Thread Starter Kevin

    (@truheart)

    Actually I forgot about this. It is a function to help with archive queries, I am assuming this might be related:

    **
     * Archive queries
     */
    function nymj_query( $query ) {
    
      if( !is_admin() ) {
    
        if( $query->is_archive() || $query->is_search() ) {
    
          $query->set( 'post_type', array(
            'post',
            'articles'
          ) );
        }
      }
    
    }
    add_filter( 'pre_get_posts', 'nymj_query' );
    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Could you try to remove that query modification, and let me know if it helps?

    Thread Starter Kevin

    (@truheart)

    Thanks, the problem is for this site I need the main archive pages and searches to retrieve posts from the “articles” custom post type I set up. I might be missing another solution to this, but since it doesn’t seem like there will be a blog any time soon I figured it would be simplest to alter the main query on these pages.

    Thread Starter Kevin

    (@truheart)

    Also, I just removed the query filter and tried the default archive for my articles post type. It gave the same result. Without any modifications to the main query, the archive is showing the initial 3 posts then infinite scroll reloads them as it initializes, so the issue must be somewhere else.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    Could you contact us via this contact form, and send us a copy of your theme so we can run some more tests?

    Thanks!

    I’m having the same exact issue but don’t see a resolution here. Was it resolved?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic πŸš€

    @amycarolyn Could you follow the instructions I posted above to make sure the problem isn’t linked to your theme?

    If that doesn’t help, could you send us a copy of your theme via this form so we can take a closer look?

    Thanks!

    It is the theme but I can’t figure out what would be conflicting. I’m not using a any custom queries (just the regular loop) and have Infinite Scroll implemented correctly in my theme as far as I can tell. I submitted a support request using the form. Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Infinite scroll repeats initial posts’ is closed to new replies.