Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
T5 Comment List Shortcode
<?php
/**
* Plugin Name: T5 Comment List Shortcode
* Description: Use [listcomments post_id="NUMBER"] to list comments from another post.
* Plugin URI:
* Version: 2013.01.10
* Author: Thomas Scholz
* Author URI: http://toscho.de
* Licence: MIT
* License URI: http://opensource.org/licenses/MIT
*/
add_shortcode( 'listcomments', 't5_list_comments' );
function t5_list_comments( $atts, $content = '' )
{
'' !== $content && $content = wpautop( do_shortcode( $content ) );
if ( ! isset ( $atts['post_id'] ) )
return 'Please pass an argument for "post_id"';
if ( ! $comments = get_comments( $atts ) )
return 'No comments found for post ID '. esc_html( $atts['post_id'] );
$out = $content . '<ul class="t5-comment-list">';
foreach ( $comments as $comment )
{
$out .= sprintf(
'<li>%1$s%2$s</li>',
get_comment_author_link( $comment->comment_ID ),
wpautop( $comment->comment_content )
);
}
return $out . '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.