2

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

5
  • You mean something like this wordpress.org/plugins/wp-auto-refresh ? – adeeb Jul 20 '13 at 3:39
  • 1
    @adeeb I don't think it's a real solution. Autorefresh is a lousy way to do it - let's say that you are reading and page refreshes... ;) – Krzysiek Dróżdż Jul 20 '13 at 18:21
  • If i undestood that right, wp-auto-refresh plugin refreshes the entire page... i need to refresh only the div where the list of posts are showed. I want to automatic show the new posts without need to reload the page... – Leibovich Jul 24 '13 at 21:55
  • I think P2 theme does this, but it's really somewhat atypical feature and likely involved to implement (everyone who has front page open would need to be somehow polling for updates...) – Rarst Feb 9 '14 at 18:11
  • I cant change the theme =(. I'm forcing the html to reload after x minutes, but that sucks. Need to found a way to use AJAX to relod only the post DIV – Leibovich Feb 10 '14 at 18:46
1

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;
}
1
  • 1
    I upvoted your answer to give you enough rep to comment. For future questions: Please avoid any questions (or other noise) in answers. Stay on topic, which will lead to more upvotes and therefore more rep points and more privileges. – kaiser Feb 12 '14 at 4:53
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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