WordPress在文章内引用评论短代码

我们可以采用短代码的方式把评论引入到文章内容中,还可以自定义引入样式,在主题的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)

你可以根据你自己的需要来调整代码,也可以自己自定义CSS样式。

至于调用就非常简单了,直接使用短代码[fa_insert_comments ids=123,245]即可

如果你不是在文章内容中,而是在其他地方想调用,则可使用do_shortcode(‘[fa_insert_comments ids=123,245]’)来调用。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容