作者:大发
原文:大发 - WordPress 文章内引用评论短代码 https://fatesinger.com/100102
前情回顾
WordPress 文章内链短代码,仿 WordPress 的 Post Embed 功能。
实现代码
下面的代码加入到functions.php中
大发原版代码▼展开
function fa_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 ( empty($fa_comments) ) return;
foreach ($fa_comments as $key => $fa_comment) {
$content .= '<div class="comment-mixtapeEmbed"><span class="comment-mixtapeEmbed-avatar">' . get_avatar($fa_comment->comment_author_email,32) . '</span><div class="comment-mixtapeEmbed-author">' . $fa_comment->comment_author . '</div><div class="comment-mixtapeEmbed-date">' . $fa_comment->comment_date .'</div><div class="comment-mixtapeEmbed-text">'. $fa_comment->comment_content . '</div></div>';
}
return $content;
}
add_shortcode('fa_insert_comments', 'fa_insert_comments');
如果想输出评论格式,则把代码中的
$fa_comment->comment_content
替换为
apply_filters('comment_text',$fa_comment->comment_content)
我修改过在用的代码(加了评论链接和所在文章)▼展开
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 ( empty($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 代码▼展开
.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
}
你可以根据你自己的需要来调整代码,也可以自己自定义 CSS 样式。调用非常简单,直接使用短代码 [xx_insert_comments ids=123,245] 即可。
如果你不是在文章内容中,而是在其他地方想调用,则可使用 do_shortcode('[xx_insert_comments ids=123,245]') 来调用。
效果
附:[[ 短代码 ]] 这样包含短代码就不会执行。
本文首发于:WordPress 文章内引用评论短代码-垃圾站