WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Auto Refresh Div with Related Posts

[Resolved] Auto Refresh Div with Related Posts

  • blurby

    @nurdanucergmailcom

    Hello,

    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() {
            $('div#refresh').fadeIn();
            var $news = $('div#news');
            $.get('<?php //echo $_SERVER[HTTP_HOST]; ?>/news.php',{action:'update'},function(data){
                $("div#news").html(data);
                $('div#refresh').fadeOut();
                newsTimeout = setTimeout(update,20000);
            });
    
        }
        update(); // let's execute it
    </script>

    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'); ?>
    
    <?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; ?>	
    
    <?php
    $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 ‘[Resolved] Auto Refresh Div with Related Posts’ is closed to new replies.