Last active
September 15, 2017 08:55
-
-
Save banago/3714766 to your computer and use it in GitHub Desktop.
WordPress - Recent Comments without a Plugin or Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bg_recent_comments($no_comments = 10, $comment_len = 35) { | |
global $wpdb; | |
$request = "SELECT * FROM $wpdb->comments"; | |
$request .= " JOIN $wpdb->posts ON ID = comment_post_ID"; | |
$request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''"; | |
$request .= " ORDER BY comment_date DESC LIMIT $no_comments"; | |
$comments = $wpdb->get_results($request); | |
if ($comments) { | |
foreach ($comments as $comment) { | |
ob_start(); | |
?> | |
<li> | |
<a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"><?php echo dp_get_author($comment); ?>:</a> | |
<?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)); ?> | |
</li> | |
<?php | |
ob_end_flush(); | |
} | |
} else { | |
echo '<li>'.__('No comments', 'banago').'</li>'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="widget recent-comments"> | |
<h3>Recent Comments</h3> | |
<?php get_recent_comments(); ?> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.recent-comments { list-style: none; font-size: 12px; color: #485358; } | |
.recent-comments li { overflow: hidden; padding: 20px 0; border-top: 1px dotted #DADEE1; } | |
.recent-comments li:first-child { border: 0 none; } | |
.recent-comments img { float: left; margin-right: 8px; } | |
.recent-comments a { display: block; font-family: 'Rokkitt',Arial,sans-serif; margin-top: 10px; padding-top: 10px; text-transform: uppercase; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Show Recent Comments | |
* | |
* @author Baki Goxhaj | |
* @link http://wplancer.com/how-to-display-recent-comments-without-using-a-plugin-or-widget/ | |
* | |
* @param string/integer $no_comments | |
* @param string/integer $comment_len | |
* @param string/integer $avatar_size | |
* | |
* @echo string $comm | |
*/ | |
function bg_recent_comments($no_comments = 5, $comment_len = 80, $avatar_size = 48) { | |
$comments_query = new WP_Comment_Query(); | |
$comments = $comments_query->query( array( 'number' => $no_comments ) ); | |
$comm = ''; | |
if ( $comments ) : foreach ( $comments as $comment ) : | |
$comm .= '<li>' . get_avatar( $comment->comment_author_email, $avatar_size ); | |
$comm .= '<a class="author" href="' . get_permalink( $comment->post_ID ) . '#comment-' . $comment->comment_ID . '">'; | |
$comm .= get_comment_author( $comment->comment_ID ) . ':</a> '; | |
$comm .= '<p>' . strip_tags( substr( apply_filters( 'get_comment_text', $comment->comment_content ), 0, $comment_len ) ) . '</p></li>'; | |
endforeach; else : | |
$comm .= 'No comments.'; | |
endif; | |
echo $comm; | |
} |
Thanks you for your snippet. It's useful. 😃
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great snippet of code! I think you may have a typo though, on line 13 of "recent-comments.php". Shouldn't the function name be "get_recent_comments" instead of "bg_recent_comments"?
This saved me some time and once I changed the function name it worked great!