Wordpress showing comments from a message received using ajax

I am downloading a message using ajax. The code

$(document).ready(function(){

    loadPostsFun = function(){
        $.ajax({
            url: "http://lab1.koalamedia.es/ajax/",
            //url: "/random/",
            success: function(response){
                $("#randomPost").html( response );
            }
        });
    };
    $("#another").click(function(){
        loadPostsFun();
        return false;
    });
});

The response is generated by a custom template using this code:

<?php
    query_posts('showposts=1&orderby=rand');
    the_post();
    $args = array( 'numberposts' => 1, 'orderby' => 'date' );
    $rand_posts = get_posts( $args );
?>
<?php
    foreach( $rand_posts as $post ) :  setup_postdata($post);
?>

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <?php if ( is_front_page() ) { ?>
        <h2 class="entry-title"><?php the_title(); ?></h2>
        <?php } else { ?>
          <h1 class="entry-title"><?php the_title(); ?></h1>
        <?php } ?>

         <div class="entry-content">
          <?php the_content(); ?>
          <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
        <?php comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); ?>
        </div><!-- .entry-content -->
      </div><!-- #post-## -->

      <?php 

        //comments_template( '', true ); //this doesn't work
        comment_form(); 
        //wp_list_comments(''); //this doesn't work

      ?>
<?php endforeach; ?>

The ajax request works, but comments are not displayed. All data messages are. How can I show comments?

neither the comments_template or wp_list_comments functions work.

You can view the demo or download the sample template that I made here

+3
source share
2 answers

I found a problem, I forgot to set a global variable:

global $withcomments;

i used

$withcomments = true; 
comments_template();

but without global it does not work.

Now works like regular comments.

+1
source

wp_list_comments() ( comments.php).

get_comments(), :

$comments = get_comments(array ( 'post_id' =>  $post->ID );
if ( $comments )
{
    foreach ( $comments as $comment )
    {
        print "<li>$comment->comment_author<br>$comment->comment_content</li>";
    }
}
+2