Auto Refresh Post List after X seconds

i’m new in wordpress, and i really need a help here. My main site, list the resume (excrept) of all posts, with pagination. But when a user publish a new post, the only way to see it on the main page is to reload it… I want to know if there’s a way to automatic show new posts , like facebook….

I didn’t found a way to do it untill now.

I’ve removed the following line from my index.php (inside the /wp-content/themes/wp_blogger/):

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php include(TEMPLATEPATH."/excrept.php"); ?>

<?php endwhile; else: ?>

    <div class="featured_section">
        <div class="featured_in">
            <h3 style="margin-bottom:800px;"><?php _e('Nenhum post encontrado com estes critérios.'); ?></h3>
        </div>
    </div>

<?php endif; ?>

I’ve put the code above inside a load.php (at the same directory), and replace it inside the index.php with:

<script>
$(document).ready(function(){
var callAjax = function(){
$.ajax({
method: "GET",
url: "/RMON/blog/wp-content/themes/wp_blogger/load.php",
success: function(data){
$("#test").html(data);  
}
});
    }   
setInterval(callAjax,5000);
});
</script>
   <div id="test"></div>

But no luck with that… i’m getting this error: Fatal error: Call to undefined function have_posts() in /var/www/RMON/blog/wp-content/themes/wp_blogger/load.php on line 1

2 Answers
2

Your answer definitely falls within the realm of AJAX. If you are using a form, try setting the onsubmit eventListener equal to return false; (plus any other needed attributes) like so:

<form onsubmit="return false;">

Then you send a post and parse it with PHP and depending on your desired effect, and the input of the user, you echo out a response to your waiting AJAX script, which can take the ajax.repsonseText and depending on whether or not the response was equal to what you were looking for, you can set the desired HTML element’s innerHTML equal to you whatever you want to without refreshing the page. More than likely something like this:

if(ajax.responseText != "error") {
    document.getElementById("userInputDiv").innerHTML = ajax.responseText;
}

Leave a Comment