4

I am making a new template and I need to place the comments template outside the wordpress loop and just above the footer in the single.php file.

I searched on google and the best answered I could find related to my issue is in this link: https://stackoverflow.com/questions/6384205/displaying-the-wordpress-comments-template-outside-the-loop

However, that did not work. The same comments are appearing on all posts.

So, how can I display the comments display outsite the wp loop?

EDIT: This is the single.php file

<?php
get_header(); ?>

<div id="primary" class="site-content">
    <div id="content" role="main">

        <?php while ( have_posts() ) : the_post(); ?>

            <?php get_template_part( 'content-single', get_post_format() ); ?>

            <nav class="nav-single">
                <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentytwelve' ); ?></h3>
                <span class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentytwelve' ) . '</span> %title' ); ?></span>
                <span class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentytwelve' ) . '</span>' ); ?></span>
            </nav><!-- .nav-single -->



        <?php endwhile; // end of the loop. ?>

    </div><!-- #content -->
</div><!-- #primary -->
<div id="secondary" class="sidebar-area" role="complementary">
    <?php get_sidebar(secondary); ?>
    <?php get_sidebar(); ?>
</div><!-- #secondary -->

<?php
comments_template( '', true );
?>

 <?php get_footer(); ?>
  • 1
    Please post whatever code you have right now that isn't working. – mrwweb Sep 16 '13 at 23:53
  • ok I just edited the post to include the code. – Gixty Sep 17 '13 at 1:26
  • Next, double-check that you've got comments enabled for whatever post you'd trying to use and that you're looking at something that uses the single.php template. – mrwweb Sep 17 '13 at 13:06
  • Yes, comments are enabled for the post I am looking at. The problem here is that the comments_template is showing up but the comments that are made appear on all posts. – Gixty Sep 17 '13 at 16:00
9

ok, after some research I came up with the solution. The solution is for displaying wordpress comments outside the loop and as a bonus how to place disqus comments outside the loop as well.

First, How to place wordpress comments outside the loop:

In single.php we need to define a new global variable for storing the post id (place this inside the loop)

global $postid;
$postid = get_the_ID();

Then, we can place the list of comments outside the loop with the following code:

<ol class="commentlist">
    <?php    
        //Gather comments for a specific page/post 
        $comments = get_comments(array(
            'post_id' => $postid,
            'status' => 'approve' //Change this to the type of comments to be displayed
        ));

        //Display the list of comments
        wp_list_comments(array(
            'per_page' => 10, //Allow comment pagination
            'reverse_top_level' => false //Show the latest comments at the top of the list
        ), $comments);

    ?>
</ol>

You can as well place the comment form and pass the post id as follows:

<?php comment_form( $args, $postid ); ?>

For DISQUS:

In single.php, We need to define a second variable to get the post title (place this inside the loop):

global $dposttitle;
$dposttitle = wp_title( '', false);

And then add the following call wherever you want to display your disqus comments:

In your child's theme functions add the following:

function disqus_embed($disqus_shortname, $postid, $dposttitle) {
    global $post;
    wp_enqueue_script('disqus_embed','http://'.$disqus_shortname.'.disqus.com/embed.js');
    echo '<div id="disqus_thread"></div>
    <script type="text/javascript">
        var disqus_shortname = "'.$disqus_shortname.'";
        var disqus_title = "'.$dposttitle.'";
        var disqus_url = "'.get_permalink($postid).'";
        var disqus_identifier = "'.$disqus_shortname.'-'.$postid.'";
    </script>';
}

Finally, call disqus_embed outside the loop in single.php

disqus_embed($disqus_shortname, $postid, $dposttitle);

Please feel free to tell me if there is a better way to implement this.

| improve this answer | |

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.