Ajax Load More Problem
-
Hello, I have a problem when related posts load on my website.
When the load more button is clicked, it loads the current post, and loads the same posts twice. and it does not load related posts at all. It loads posts from different categories. Though it should load from the same category first. Here is the code I am using.<!-- related posts start --> <?php // Get the categories for the current post $categories = get_the_category( get_the_ID() ); // Get an array of category IDs $category_ids = array(); foreach ( $categories as $category ) { $category_ids[] = $category->term_id; } // Get the tags for the current post $tags = wp_get_post_tags( get_the_ID() ); // Get an array of tag IDs $tag_ids = array(); foreach ( $tags as $tag ) { $tag_ids[] = $tag->term_id; } // Query for related posts based on the categories and tags $related_posts = get_posts( array( 'category__in' => $category_ids, 'tag__in' => $tag_ids, 'post__not_in' => array( get_the_ID() ), // exclude the current post from the related posts 'posts_per_page' => 12 // Change this number to set the number of related posts to display ) ); // If there are related posts, display them if ( $related_posts ) : ?> <div class="container"> <!-- container start --> <div class="row overflow-hidden publication-list"> <!-- row start --> <?php foreach ( $related_posts as $post ) : setup_postdata( $post ); ?> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-4 col-xl-3"> <a href="<?php the_permalink(); ?>"> <div class="title-box mx-auto mb-0"> <h1 class="title-color"><?php the_title(); ?></h1> <?php if( has_post_thumbnail() ): the_post_thumbnail('homepage-thumbnail', array('class' => 'img-fluid')); else: ?> <img class="img-fluid" src="<?php echo get_template_directory_uri() . "/images/bg.jpg"; ?>"> <?php endif; ?> <?php $video_type = get_field('post_badge'); if($video_type!='select_type'): ?> <div class="post-badge"><?php echo get_field('post_badge'); ?></div> <?php endif; ?> </div> </a> </div> <?php endforeach; ?> <?php wp_reset_postdata(); ?> </div> <div class="btn__wrapper custom_loadmore_btn_div"> <a href="javascript:void(0);" class="btn btn__primary custom_loadmore_btn" id="load-more">Load more <i class="fa-solid fa-bolt-lightning"></i></a> </div> </div> <!-- container ends --> <script> let currentPage = 1; let loadedPosts = [<?php echo get_the_ID(); ?>]; jQuery('#load-more').on('click', function() { currentPage++; jQuery.ajax({ type: 'POST', url: '/wp-admin/admin-ajax.php', dataType: 'json', data: { action: 'weichie_load_more', paged: currentPage, post__not_in: loadedPosts }, success: function (res) { if(currentPage >= res.max) { jQuery('#load-more').hide(); } jQuery('.publication-list').append(res.html); loadedPosts = loadedPosts.concat(res.postIds); } }); }); </script> <!-- related posts end --> <?php endif; ?>
The page I need help with: [log in to see the link]
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Ajax Load More Problem’ is closed to new replies.