Show Recent Comments Anywhere Anyposts Without Plugins | FuSusu

Show Recent Comments Anywhere Anyposts Without Plugins...

1388 views

Comments are great, they make people understand the real quality of your products, even your blogs. WordPress has a default recent comments widget, but it’s somewhat limited. What if you want to show the recent comments right below the second paragraph of your post? That’s why I’m looking for a way to show recent comments anywhere I want.

Show Recent Comments Anywhere With A Plugin

On the way to find solutions, I found this Better Recent Comments plugin. It really did what it said. This handy plugin not only improves the default recent comments widget with maximum customization, but also helps you show recents comments anywhere with a shortcode.
show recents comments anywhere in the posts or pages with better recent comments
And the thing that surprised me most, this plugin is completely free. You can see the demo, basic usage and download below. They really did a good job, but I’m still a fan of coding myself. So I decided to make my own shortcode. If you’re like me, then keep reading to find the treasure.
show recents comments anywhere in the posts or pages with better recent comments

So you’re a fan of coding? Let’s dive into the journey I found the treasure!

Note There is two parts below. The first part is the journey I found the code. You can skip it if you want, but it helps you understand the code, so you can customize your own. The second part is the bone, you can apply right away to your site.

PART 1 – My journey to complete this quest.

Show Recent Comments Anywhere Without Plugins – 1st Milestone – Make a safe testing page (you can skip this step if you want)

Normally, to safely test everything, you should make a custom template page in your theme. Let’s create a new file, named it susucomments.php , then add the code below and make it become a template page for us to use later.

<?php // Template Name: Get Recent Comments
    // Author: Fususu.com 
    // Version: v1.0 
get_header(); ?>
<!-- we will insert the main codes here later -->
<?php get_footer(); ?>

Create a new page, and set the page attribute to our newly created template, publish it. Then if you see a blank page with no sidebar or even comment section, we’re on the right track.

Now we’ll try to make some comments on this page for testing purpose. To enable default comments section, look into your single.php or page.php in theme folder.

Then copy the loop content section. In my theme, the codes looks like below. The most important things here are the while loop, the content, and the comment templates. Now I just copy them and paste to my template.

<section class="content">
    <div class="pad group">	
    <?php while ( have_posts() ): the_post(); ?>
                <article>
                    <div class="post-inner group">
                        <div class="content">
                            <div class="entry-inner">
                                <?php the_content(); ?>								
                            </div>			
                        </div>
                    </div><!--/.post-inner-->
                </article><!--/.post-->
                <?php comments_template('/comments.php',true); ?>
    <?php endwhile; ?>
    </div>
</section>

Now the test page is done. We can write something on it, make comment like a normal post or page. We’ll move to step 2: Try to query recent comments of that page, and put it… on the top!

Show Recent Comments Anywhere Without Plugins – 2nd Milestone – Try to query recent comments of current page or post

After a small research, I found this post with lots of useful information on how to query comments from wordpress database.

With its help, now I can query the comments of the current page myself with full control. We’re moving closer to our goal. This code will show us 3 newest comments from all the posts.

<?php  // Arguments for the query                                            
             $args = array(          
                    'number' => 3,
           );

           // The comment query
       $comments_query = new WP_Comment_Query;
       $comments = $comments_query->query( $args );

   // The comment loop
   if ( !empty( $comments ) ) {
         foreach ( $comments as $comment ) {
           echo '<p><strong>' . $comment->comment_author . '</strong> says "';
            echo '' . $comment->comment_content . '"</p>';      
     }
  } ?>

If you change $args bit, you can restrict the comments to current post or page. This code below will show 3 newest comments of the current page or post.

<?php           // Arguments for the query                                            
                                $current_post_id = get_the_ID();
                                $args = array(
                                    'post__in' => $current_post_id,
                                    'number' => 3,
                                );                  

With the tests above, I strongly believe we can make a function that can query recent comments of any current post page.

Show Recent Comments Anywhere Without Plugins – 3rd Milestone – Try to make a shortcode to get recent comments anywhere

To do this, we have to mess up with functions.php a bit. So I suggest you make a beta site for testing purpose to prevent disrupting your readers’ experience.

First, I tried to query the recent comments of all posts by put this code at the end of my functions.php in the theme folder, and make a shortcode.

function susu_recent_comments() {
    //$current_post_id = get_the_ID();
    $args = array(
     // 'post__in' => $current_post_id,
      'number' => 3,
   );

    // The comment query
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );

     // The comment loop
    if ( !empty( $comments ) ) {
          foreach ( $comments as $comment ) {
          echo '<p><strong>' . $comment->comment_author . '</strong> says "';
          echo '' . $comment->comment_content . '"</p>';                    
         }
           } else {
     echo 'No comments found.';
   }
}
add_shortcode('susucomments', 'susu_recent_comments');

The result is great! Now I can show 3 newest comments by just using this shortcode [ susucomments ] in the any post or page. That’s a huge step to success! To make the shortcode more flexible, just add some attributes, the we can have a handy shortcode!

PART 2 – The complete code to show recent comments of any post or page anywhere, now you can have it!

After some more tests, I finally found the gem. Just put this code at the end of your functions.php. It will help you show recent comments of any post or page, anywhere you want.

function susu_recent_comments($att) {
    extract(shortcode_atts(array(
        'number' => '',
        'current' => '',
    ), $att));
    $args = array();
    if (empty($current)) { 
        $args = array(
          'number' => $number,
        );
    } else {
         if ($current == 0) {
             $current_post_id = get_the_ID();
             $args = array(
              'post__in' => $current_post_id,
              'number' => $number,
            );
         } else {
             $args = array(
              'post__in' => $current,
              'number' => $number,
            );
         }
    }
    // The comment query
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );

     // The comment loop
    if ( !empty( $comments ) ) {
          foreach ( $comments as $comment ) {
          echo '<strong>' . $comment->comment_author . '</strong> said "';
          echo '' . $comment->comment_content . '"</p>'; 
          echo '<p><small>On <strong>' . $comment->comment_date . '</strong></small></p>';
         }
           } else {
     echo 'No comments found or there is no post with that ID.';
   }
}
add_shortcode('susucomments', 'susu_recent_comments');

Basic usage, put this shortcode anywhere you want to show recent comments.

Note Remember to remove the space between “[” and “susucomments” to make it work, if you copy/paste the code.

[ susucomments number=x ]

number = x is the comment you want to show.

If you want to show the recent comments current post

[ susucomments number=x current="0" ]

If you want to show the recent comments of any post or page, just replace current with the post or page ID (you get the ID by look into the Url when edit that post or page).

[ susucomments number=x current="your_specific_id" ]

That’s it! Now you know how to show recent comments of any posts anywhere.

Of course, there is more works to be done. You should format the comment date, and write some CSS to make the comments look nicer, or how to make this shortcode work in sidebar? But it’s just enough for today, see you in the next post!

P.s. Some last words, you really should try the free plugin I introduced at first, with lots of cool features. They really did a good job, and made me think I shouldn’t re-invent the wheel!

show recents comments anywhere in the posts or pages with better recent comments

TREASURE CHEST

!!!



You can wait for to get the hidden content, or just share to see it now. Sharing is the key to succeed!

UNLOCK NOW!

Have a good day!
Fususu wrote 19/10/2019. Updated 19/10/2019.


P.s. To motivate me, you can google this keyword show recent comments anywhere, find my blog there then comment the position. Thank you so much!

Don't hesitate to leave a comment, I'll reply in... 86400s!


Leave a Reply

Your email address will not be published. Required fields are marked *