0

I am loading posts through ajax and it seems new load produces duplicate posts on archive standard loop. This my code I have tried several methods with $do_not_duplicate but it seems none worked or I had wrong code in place. This is the code for my standard loop:

<?php

$cats = get_the_category();
$cats_array = array();

if ( ! empty( $cats ) ) {
  foreach ( $cats as $cat ) {
    $cats_array[] = '<a href="' . get_category_link( $cat->term_id ) . '">' . $cat->name . '</a>';
  }
}

$cats_output = ( ! empty( $cats_array ) ) ? ' ' . join( ', ', $cats_array ) : '';

?>
<article <?php post_class( 'loop-post loop-post--standard' ); ?>>
  <div class="entry-header">
    <div class="container-fluid container-fluid--small">
      <div class="d-flex align-items-center mb-2">
        <?php echo theme_get_post_categories( null, 'mb-0' ); ?>

      </div>
      <h2 class="entry-title loop-post__title mb-3"><a href="<?php the_permalink( $post_id ); ?>"><?php the_title(); ?></a></h2>
      <?php echo theme_post_meta( true, true, false, false ); ?>
    </div>
  </div>
  <?php if ( has_post_thumbnail() ): ?>
    <div class="entry-featured loop-post__image">
      <a href="<?php the_permalink(); ?>" data-theme-bg-image>
        <?php the_post_thumbnail( 'entry-large' ); ?>
      </a>
    </div>
  <?php endif; ?>
  <div class="post-cart__excerpt">
    <div class="container-fluid container-fluid--small">
      <p class="mb-0"><?php echo get_the_excerpt(); ?></p>
    </div>
  </div>
</article>

Ajax for loading posts:

jQuery(document).ready(function($) {
  const $win = $(window),
        $ajaxTrigger = $('.theme-ajax-loader__trigger');

  $ajaxTrigger.on('click', function (e) {

    e.preventDefault();

    let $this = $(this),
        buttonText = $this.text(),
        $posts = $('.theme-archive__posts');

    if ( $posts.length ) {
      var page = 2;

      $.ajax({

        url: themeajax.ajax_url,
        type: 'post',
        data: {
          action: 'theme_ajax_handler',
          theme_handler: 'load_more_posts',
          theme_ajax_nonce: themeajax.ajax_nonce,
          // page: page,
          query: themeajax.posts,
          page: themeajax.current_page,
          layout: 'standard'
        },
  • Please post the code you're using for the ajax pagination The template code above doesn't relate to your question. – MikeNGarrett Jun 13 '19 at 15:51
  • @MikeNGarrett Here is the JS code the question is edited. Hopefully it is correct code as I am not JS expert. Thanks. – Benjamin Franklin Jun 13 '19 at 19:01

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.