Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently loading a single post using ajax. While the post loading works ok, I cannot get the comments to load. Here is my code:

My javascript to load the post:

<script>
$(".view_post").click(function(e) {
    e.preventDefault();
    postid = $(this).attr("rel");
                $.ajax({
                    url:"/wp-admin/admin-ajax.php",
                    type:'POST',
                    data:'action=posts_open&postid='+postid,
                    success: function(html){
                        $("#b_contentwrapper").empty();
                        $("#b_contentwrapper").append(html);
                    }
                });
            });
</script>

The javascript goes through functions.php this way:

function implement_posts()
{
    //<?php
    get_template_part( 'loop', 'single' );
    die();
}

Now here is the code where I actually load my post content:

<?php
    $linkid = "p=".$_POST["postid"];
    $posti = new WP_Query($linkid);
    $posti->the_post();
    echo "Time: ";
    the_time('F jS, Y');
    echo "<br />";
    the_category(', ');
    echo "<br />";
    the_title();
    echo "<br />";
    the_content();
    echo "<br />";
    comment_form();
    ?>
    </div>
    <?php if (have_comments()) {
    echo "Comments ok";
    }
    else
    {
        echo "No comments";
    }
    ?>

Now, even for posts having comments I am getting "No comments" displayed. Everything else works correctly. Can anyone help me out?

Thank you.

share|improve this question
$linkid = "p=".$_POST["postid"]; is not verry sql injection safe. although wordpess checks this you might wanna do it yourself. – RTB Jul 31 '12 at 22:49

2 Answers

On my opinion, it would be better to go with a JQuery .load($[this].attr('href') '.div-with-content-and-comment');

Than make sure you have a single.php that has the markup with the class="div-with-content-and-comment" you want to load via ajax.

share|improve this answer

Look at the source of have_comments() - this check retrieves data from global $wp_query object, which is not used in your case.

So the first step would be to replace have_comments() check with $posti->have_comments().

share|improve this answer

protected by kaiser Jun 10 at 1:02

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

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