Ubuntu java Firefox Windows linux命令 nginx linux 微软 shell mysql php Android wordpress 开源 程序员 centos HTML5 Python apache google

WordPress & Typecho 评论回复

类似于“WordPress回复评论自动加上@符号”,采用jQuery实现的,本文实现方法为PHP,都是针对自定义评论结构来实现的,由于Wordpress Api丰富,实现方法相对简单,Typecho就比较麻烦了。

预览图在下面:

牧风

WordPress版本

function mfthemes_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment;
?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<div id="comment-<?php comment_ID(); ?>" class="comment-body">
			<div class="comment-avatar">
				<?php echo get_avatar( $comment->comment_author_email, $size = '28'); ?>
				<div class="comment-reply"><?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth'], 'reply_text' => "@"))) ?></div>
			</div>
			<div class="comment-content">
				<p><span class="comment-author"><?php printf(__('%s'), get_comment_author_link()) ?></span>
 
				<!--重点就是这里啦-->
				<?php if($comment->comment_parent){// 如果存在父级评论
					$comment_parent_href = get_permalink($comment->comment_post_ID). "#comment-" . $comment->comment_parent;
					$comment_parent = get_comment($comment->comment_parent);
					?>
					<span class="comment-to plr">回复</span>
					<span class="comment-author"><a href="<?php $comment_parent_href;?>" title="<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $comment_parent->comment_content)), 0, 100,"..."); ?>"><?php echo $comment_parent->comment_author;?></a></span>
				<?php }?>
				<!-- END-->
 
				</p>
				<?php if ( $comment->comment_approved == '0' ) : ?>
					<em><?php _e( 'Your comment is awaiting moderation.' ); ?></em><br />
				<?php endif; ?>
				<?php comment_text() ?>
			</div>
		</div>
<?php }

Typecho版本

function getPermalinkFromCoid($coid) {
		$db       = Typecho_Db::get();
		$options  = Typecho_Widget::widget('Widget_Options');
		$contents = Typecho_Widget::widget('Widget_Abstract_Contents');
 
		$row = $db->fetchRow($db->select('cid, type, author, text')->from('table.comments')
				  ->where('coid = ? AND status = ?', $coid, 'approved'));
 
		if (empty($row)) return 'Comment not found!';
		$cid = $row['cid'];
 
		$select = $db->select('coid, parent')->from('table.comments')
				  ->where('cid = ? AND status = ?', $cid, 'approved')->order('coid');
 
		if ($options->commentsShowCommentOnly)
			$select->where('type = ?', 'comment');
 
		$comments = $db->fetchAll($select);
 
		if ($options->commentsOrder == 'DESC')
			$comments = array_reverse($comments);
 
		foreach ($comments as $key => $val)
			$array[$val['coid']] = $val['parent'];
 
		$i = $coid;
		while ($i != 0) {
			$break = $i;
			$i = $array[$i];
		}
 
		$count = 0;
		foreach ($array as $key => $val) {
			if ($val == 0) $count++; 
			if ($key == $break) break; 
		}
 
		$parentContent = $contents->push($db->fetchRow($contents->select()->where('table.contents.cid = ?', $cid)));
		$permalink = rtrim($parentContent['permalink'], '/');
 
		$page = ($options->commentsPageBreak)
			  ? '/comment-page-' . ceil($count / $options->commentsPageSize)
			  : ( substr($permalink, -5, 5) == '.html' ? '' : '/' );
 
		return array(
			"author" => $row['author'],
			"text" => $row['text'],
			"href" => "{$permalink}{$page}#{$row['type']}-{$coid}"
		);
	}	
 
	/** 自定义评论 **/
    function threadedComments($comments,$singleCommentOptions) {
			$author = '<a href="'.$comments->url.'" rel="external nofollow" target="_blank">'.$comments->author.'</a>';
        ?>
	<li id="<?php $comments->theId(); ?>">
		<div id="<?php $comments->theId(); ?>">
            <div  class="comment-body">
			    <div class="comment-meta"><?php $comments->gravatar('44','');?></div>
				<div class="commentmetadata">&nbsp;-&nbsp;<?php $comments->date('Y-m-d H:i:s') ?></div>
				<div class="comment-reply"><?php $comments->reply('@') ?></div>
				<div class="vcard">
					<?php echo $author;?>
 
					<!--重点就是这里啦-->
					<?php
						if($comments->parent){
							$p_comment = getPermalinkFromCoid($comments->parent);
							$p_author = $p_comment['author'];
							$p_text = mb_strimwidth(strip_tags($p_comment['text']), 0, 100,"...");
							$p_href = $p_comment['href'];
							echo "<span>回复</span><a href='$p_href' title='$p_text'>$p_author</a>";
						}
					?>
					<!-- END-->
 
				</div>
				<?php $comments->content(); ?>
            </div>
            <div class="children">
                <?php if ($comments->children) { ?><?php $comments->threadedComments($singleCommentOptions); ?><?php } ?>
            </div>
		</div>
	</li>
<?php }

延伸阅读

评论