Hey guys, last days i have been trying to figure out how to do that. As read on http://net.tutsplus.com/tutorials/javascript-ajax/create-a-twitter-like-load-more-widget/ but inside wordpress. And i have finally come with a solution, may not be the best one but i hope you guys can improve my code and post it here just like i`m doing. Feel free to use it wherever you want. (my coding skills are really bad so

First you need to include jquery on your header.

Insert this code on the page you wanna display the posts:

$number_of_posts = 3;
if (!$_SESSION['posts_start']) { $_SESSION['posts_start'] = 0; }
$_SESSION['posts_start'] = $_SESSION['posts_start'] ? $_SESSION['posts_start'] : $number_of_posts;
$total_posts = wp_count_posts('post');
$total_posts = $total_posts->publish;
<script type="text/javascript">
        var initialPosts = <?php echo my_get_posts(0, 5); ?>;
        var postHandler = function(postsJSON) {     
            jQuery.each(postsJSON,function(i,post) {
                // Salva as variáveis de cada post
                var postURL = post.permalink;
                var postCOMMENTS = post.comments;
                var postTITLE = post.title;
                var postDATE = post.date;
                var postAUTHOR = post.author;

                // Coloca as variáveis na lista                 
                .append("<li><span class=\"comentarios-artigo\">" + postCOMMENTS + "</span><h2><a href=\"" + postURL +"\">" + postTITLE + "</a></h2><span class=\"data-artigo\">Postado em " + postDATE +  " por " + postAUTHOR + "</span></li>")

        var totalposts = <?php echo $total_posts; ?>;
        var start = <?php echo $_SESSION['posts_start']; ?>;
        var desiredPosts = <?php echo $number_of_posts; ?>;
                url: 'http://yoursite/wp-content/themes/yourtheme/mais-posts.php',
                data: {
                    'start': start,
                    'desiredPosts': desiredPosts
                type: 'get',
                dataType: 'json',
                cache: false,
                success: function(responseJSON) {
                    jQuery('#load-more').text('Load More');
                    start += desiredPosts;
                error: function() {
                    jQuery('#load-more').text('Ops! Try Again.');
                complete: function() {
                    if (totalposts <= start) { jQuery('#load-more').hide(); }

Create a php file inside your themes folder (mine is called mais-posts.php) with the following code:

    require( '../../../wp-load.php' );

    if(isset($_GET['start'])) {
        echo my_get_posts($_GET['start'], $_GET['desiredPosts']);
        $_SESSION['posts_start'] += $_GET['desiredPosts'];

I hope it may help anyone who is looking for something like this.

So if you have any suggestion to this code please post below.

  • Did(do) you have a question somewhere in there?
    – t31os
    Commented Apr 5, 2011 at 15:20
  • This code does not work. Where is the function my_get_posts($start, $desired_posts) declared?
    – Ben
    Commented Jul 22, 2011 at 11:51

1 Answer 1


For starters see AJAX in Plugins. WordPress has own mechanism for processing Ajax request and that is what you should use over includging wp-load.php.

  • definatly useful rarst, didnt know that. Ill be taking a look to improve the code. If you have any suggestions please post it hehe Commented Apr 4, 2011 at 10:47
  • @Vitor Argos I am not very good with JS and your code is mostly that, so that is about limit of what I can advise. :) Codex is definitely a good start, Ajax implementation in WP is very WP-specific, but allows to integrate is almost seamlessly with PHP code.
    – Rarst
    Commented Apr 4, 2011 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.