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
Your answer definitely falls within the realm of AJAX. If you are using a form, try setting the
onsubmit eventListener
equal toreturn false;
(plus any other needed attributes) like so: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’sinnerHTML
equal to you whatever you want to without refreshing the page. More than likely something like this:That should be trivial to do with AJAX (it was invented to do exactly this kind of things). You need some javascript code that will send an AJAX request that asks for the latest post every X seconds and and change the content of you div with the returned content (or be smarter and add new div when it is new content)
On the server side you will need add an handler to handle you specific request and return the content of the latest post. See this codex page http://codex.wordpress.org/AJAX_in_Plugins to learn how to do it the right way.