M
M
Mazino2018-07-03 10:20:02
WordPress
Mazino, 2018-07-03 10:20:02

How to fix ajax pagination when loading posts via ajax?

In general, I made a widget on the archive page that displays taxonomy elements and, when clicked, displays posts via ajax. The difficulty is that pagination breaks, which, for any request other than query_posts, refuses to appear at all, and with it loads the same posts that are already loaded (duplicates).
Post output and pagination code:

<div id="blog-list" <?php if ( $exm1_category_post_style == 'style_1' ){echo 'class="blog-category"';}elseif( $exm1_category_post_style == 'style_2' ){echo 'class="exm1-blog-posts-category"';}elseif( $exm1_category_post_style == 'style_3' ){echo 'class="img-featured"';}?>>
      <ul>		
      <?php if (have_posts()) : while (have_posts()) : the_post();?>								
        <li>					
        <?php if ( $exm1_category_post_style == 'style_1' ){	
          category_post_style1($author_show, $date_show);	
          }elseif( $exm1_category_post_style == 'style_2' ){
          category_post_style2($author_show, $date_show);
          }elseif( $exm1_category_post_style == 'style_3' ){
          category_post_style3($author_show, $date_show);	
          }?>			
        </li>
        <?php endwhile; else : ?>
          <div class="widget-title"><?php echo esc_html(get_option('exm1_no_match')); ?></div>
        <?php endif;?>
      </ul>
    </div>
    <?php $exm1_pagination_style = get_option('exm1_pagination_style');if($exm1_pagination_style =='ajax' || $exm1_pagination_style =='auto-load' ){?>
    <div class="pagination pagination-load-more <?php if($exm1_pagination_style =='auto-load'){echo esc_attr('auto-load');}?>">
      <?php $loadmoreword = get_option('exm1_word_load_more');
      next_posts_link(esc_html($loadmoreword), '' ); ?>
    </div>
    <!--pagination-->
    <?php } else { ?>
    <div class="pagination-simple">
      <?php exm1_pagination(); ?>
    </div>
    <!--pagination-simple-->
    <?php } ?>

The problem, it seems to me, is in the interaction of the script with next_posts_link , it is he who is responsible for the link that hangs on the button.
When loading the page with a normal transition, the link looks like mysite.com/category/news/page/2 ,
but after the posts are loaded via ajax it becomes like mysite.com/wp-admin/admin-ajax.php?action=get_cat&.. . .
I understand that the function receives the link in the wrong state, but js changes the address bar even before the ajax request is executed, and I can’t understand how it happens. How to fix it without crutches?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question