WordPress文章内容通过短代码展示指定评论
在文章中引用评论,就是通过短代码在文章中插入博客评论,免去直接截图评论的麻烦,在引用时,添加了评论链接和所在文章链接,还能推推旧文章。
在主题的复制functions.php文件中,添加以下代码:
function xx_insert_comments( $atts, $content = null ){
extract( shortcode_atts( array(
'ids' => ''
),$atts ) );
$content = '';
$comment_ids = explode(',', $ids);
$query_args = array('comment__in'=>$comment_ids,);
$fa_comments = get_comments($query_args);
if ( emptyempty($fa_comments) ) return;
foreach ($fa_comments as $key => $fa_comment) {
$content .= '<div class="comment-mixtype-embed"><span class="comment-mixtype-embed-avatar">' . get_avatar($fa_comment->comment_author_email,32) . '</span><div class="comment-mixtype-embed-author"><a href="' . get_permalink($fa_comment->comment_post_ID).'#comment-' . $fa_comment->comment_ID . '">' . $fa_comment->comment_author . '</a> - <a href="' . get_permalink($fa_comment->comment_post_ID) . '">' . get_the_title($fa_comment->comment_post_ID) . '</a></div><div class="comment-mixtype-embed-date">' . $fa_comment->comment_date .'</div><div class="comment-mixtype-embed-text">'. $fa_comment->comment_content . '</div></div>';
}
return $content;
}
add_shortcode('xx_insert_comments', 'xx_insert_comments');
CSS样式
这个是作者给出的参考css,大家可以根据自己的想法修改,适合主题风格才更好看哦。
.comment-mixtype-embed {
background: #f6f6f6;
border: 1px solid #e5e5e5;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
color: #82878c;
font-size: 14px;
overflow: auto;
padding: 16px;
margin-bottom: 16px;
}
.comment-mixtype-embed-avatar {
float: left;
margin-right: 10px
}
.comment-mixtype-embed-avatar .avatar {
border-radius: 100%;
width: 32px;
height: 32px;
border: none;
margin: auto;
}
.comment-mixtype-embed-author {
font-size: 12px;
color: #3e8432;
line-height: 1.4
}
.comment-mixtype-embed-date {
line-height: 1.2;
font-size: 12px;
color: rgba(0,0,0,.44)
}
.comment-mixtype-embed-text {
margin-top: 10px;
font-size: 12px;
color: rgba(0,0,0,.6)
}
.comment-mixtype-embed-text p {
margin-bottom: 2px!important
}
引用方式
1、文章编辑时调用
使用以下短代码调用即可,ids后面数字即为要引用的评论id
[xx_insert_comments ids=123,245]
2、代码文件里调用
可使用以下短代码调用即可,ids后面数字即为要引用的评论id
do_shortcode('[xx_insert_comments ids=123,245]')