I’ve had a look through the old questions and tried many of the different methods that there seems to be to do this. The closest I’ve got to working is this one here: How to implement pagination on a custom WP_Query Ajax
I’ve tried everything and it just doesn’t work. Absolutely nothing changes on the page. If you inspect the Load More Button and click it, the jQuery is making the Load More Button action as it changes from <a id="more_posts">Load More</a>
to <a id="more_posts" disables="disabled">Load More</a>
which even that doesn6’t seem right to me anyway. It’s not adding the posts, I think I’m missing something simple but for the life of me I can’t work it out.
The code in my template file is:
<div id="ajax-posts" class="row">
<?php
$postsPerPage = 3;
$args = [
'post_type' => 'post',
'posts_per_page' => $postsPerPage,
'cat' => 1
];
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<div class="small-12 large-4 columns">
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
</div>
<?php
endwhile;
echo '<a id="more_posts">Load More</a>';
wp_reset_postdata();
?>
</div>
The code in my functions file is:
function more_post_ajax(){
$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
header("Content-Type: text/html");
$args = [
'suppress_filters' => true,
'post_type' => 'post',
'posts_per_page' => $ppp,
'cat' => 1,
'offset' => $offset,
];
$loop = new WP_Query($args);
while ($loop->have_posts()) { $loop->the_post();
the_content();
}
exit;
}
add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
And my jQuery in the footer is:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready( function($) {
var ajaxUrl = "<?php echo admin_url('admin-ajax.php')?>";
// What page we are on.
var page = 5;
// Post per page
var ppp = 3;
$("#more_posts").on("click", function() {
// When btn is pressed.
$("#more_posts").attr("disabled",true);
// Disable the button, temp.
$.post(ajaxUrl, {
action: "more_post_ajax",
offset: (page * ppp) + 1,
ppp: ppp
})
.success(function(posts) {
page++;
$("#ajax-posts").append(posts);
// CHANGE THIS!
$("#more_posts").attr("disabled", false);
});
});
});
</script>
Can anybody see something I’m missing or able to help?
UPDATE 24.04.2016.
I’ve created tutorial on my page https://madebydenis.com/ajax-load-posts-on-wordpress/ about implementing this on Twenty Sixteen theme, so feel free to check it out 🙂
EDIT
I’ve tested this on Twenty Fifteen and it’s working, so it should be working for you.
In index.php (assuming that you want to show the posts on the main page, but this should work even if you put it in a page template) I put:
This will output 3 posts from category 8 (I had posts in that category, so I used it, you can use whatever you want to). You can even query the category you’re in with
This will give you the category id to use in your query. You could put this in your loader (load more div), and pull with jQuery like
And pull the category with
But for now, you can leave this out.
Next in functions.php I added
Right after the existing
wp_localize_script
. This will load WordPress own admin-ajax.php so that we can use it when we call it in our ajax call.At the end of the functions.php file I added the function that will load your posts:
Here I’ve added paged key in the array, so that the loop can keep track on what page you are when you load your posts.
If you’ve added your category in the loader, you’d add:
And instead of 8, you’d put
$cat
. This will be in the$_POST
array, and you’ll be able to use it in ajax.Last part is the ajax itself. In functions.js I put inside the
$(document).ready();
enviromentSaved it, tested it, and it works 🙂
Images as proof (don’t mind the shoddy styling, it was done quickly). Also post content is gibberish xD
UPDATE
For ‘infinite load’ instead on click event on the button (just make it invisible, with
visibility: hidden;
) you can try withThis should run the
load_posts()
function when you’re 100px from the bottom of the page. In the case of the tutorial on my site you can add a check to see if the posts are loading (to prevent firing of the ajax twice), and you can fire it when the scroll reaches the top of the footerNow the only drawback in these cases is that you could never scroll to the value of
$(document).height() - 100
or$('footer').offset().top
for some reason. If that should happen, just increase the number where the scroll goes to.You can easily check it by putting
console.log
s in your code and see in the inspector what they throw outAnd just adjust accordingly 😉
Hope this helps 🙂 If you have any questions just ask.
If I’m not using any category then how can I use this code? Actually, I want to use this code for custom post type.