Support » Fixing WordPress » Auto Refresh Div with Related Posts

  • Resolved blurby



    I am trying to auto-refresh a div in the single post page in certain intervals. This div holds a bunch of related post items, coming through this plugin.

    I have put the related posts code bit in another template, and am calling it from inside the loop. When I call the template using get_template_part(), it works like a charm, but I cannot achieve the same result via jQuery load() function –it returns nothing at all.

    Here’s the script part in header.php (jQuery already loaded):

    <script type="text/javascript">
        var newsTimeout = 0;
        function update() {
            var $news = $('div#news');
            $.get('<?php //echo $_SERVER[HTTP_HOST]; ?>/news.php',{action:'update'},function(data){
                newsTimeout = setTimeout(update,20000);
        update(); // let's execute it

    Here’s the code bit in the single post page (which works only when I include the template inside, but not when I try to load it through the script):

    <div id="news"></div>

    And finally, here’s the “news.php” which holds the related posts, and should load dynamically into the div above (but doesn’t):

    <?php include('c:\wamp\www\test\wp-blog-header.php'); ?>
    global $post;
    $original_post = $post;
    $related = cptr_populate($post->ID);
    foreach ( $related as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>" class="jta-tweet-body"><?php the_title(); ?></a>
    <?php endforeach; ?>	
    $post = $original_post; setup_postdata($post);

    How come this works perfectly when called from inside the parent template but not load when called from the jQuery script?

    I couldn’t get it to work for the life of me, and humbly ask for your help.

    Many thanks in advance.

  • The topic ‘Auto Refresh Div with Related Posts’ is closed to new replies.