I’ve developed custom theme from scratch with _S starter theme. I’m having issue getting WordPress next posts via ajax request on click on read more button. I’ve tried many articles specifically the following articles, but no luck.
Reference Links:
- Load More Posts Ajax Button in WordPress
- Load Next WordPress Posts With AJAX
- Load Old WordPress Posts on the Same Page with AJAX
I’ve tried going with above custom loops and adding custom functions with jquery script but it don’t work somehow.
Here is loop code example below:
Loop in index.php
<?php query_posts('showposts=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post-item">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<h2>Sorry no posts are created yet.</h2>
<p>Please create some posts to see it in action.</p>
<?php endif; wp_reset_query(); ?>
<button class="load-more-btn">Load More</button>
I’m messing with this issue for over 4-5 days, so any one can help me out with this issue with working solution will be highly appreciable.
Thanks in advance.
I maybe have a solution for your.
First be sure to have a script enqueue in your theme
then localize add a function to add a js var in your dom
In your js, add an event on ‘click’ on your ‘Load More’ button
Pass an action name and the count of article you have in your dom, the response add the content before your button ‘load more’
create a function in your functions.php
It work for me.
I hope that help you.