rin-wendy.com情侣模板的制作思路(二)

上一篇分析的是情侣模板的基本布局,接下来就是评论部分。主要是判断非作者、作者A和作者B,还有奇偶的问题。

1. 修改comments.php,在wp_list_comments代码那加上&callback=my_comment。

1
<?php wp_list_comments('type=comment&callback=my_comment'); ?>


2. 修改function.php,加入如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function my_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment; ?>
	<li <?php love_class(); ?> id="li-comment-<?php comment_ID() ?>">
		<div id="comment-<?php comment_ID(); ?>">
			<div class="comment-text">
				<div class="comment-text-top">&nbsp;</div>
				<?php comment_text() ?>
 
				<?php if ($comment->comment_approved == '0') : ?>
				<em><?php _e('Your comment is awaiting moderation.') ?></em>
				<?php endif; ?>
 
				<?php edit_comment_link(__('(Edit)'),'  ','') ?>
				<div class="comment-text-bot">&nbsp;</div>
			</div>
 
			<div class="comment-author vcard">
				<?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
				<?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
			</div>
 
			<div class="comment-meta commentmetadata">
				<?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>
			</div>
		</div>
}

3. function.php,加入如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function love_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
	// Separates classes with a single space, collates classes for comment DIV
	$class = 'class="' . join( ' ', love_comment_class( $class, $comment_id, $post_id ) ) . '"';
	if ( $echo)
		echo $class;
	else
		return $class;
}
 
function love_comment_class( $class = '', $comment_id = null, $post_id = null ) {
	global $comment_alt, $comment_depth, $comment_thread_alt;
 
	$comment = get_comment($comment_id);
 
	$classes = array();
 
	// Get the comment type (comment, trackback),
	$classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
 
	// If the comment author has an id (registered), then print the log in name
	if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
		// For all registered users, 'byuser'
		$classes[] = 'byuser comment-author-' . $user->user_nicename;
		// For comment authors who are the author of the post
		if ( $post = get_post($post_id) ) {
			if ( $comment->user_id === $post->post_author )
				$classes[] = 'bypostauthor';
		}
	}
 
	if ( empty($comment_alt) )
		$comment_alt = 0;
	if ( empty($comment_depth) )
		$comment_depth = 1;
	if ( empty($comment_thread_alt) )
		$comment_thread_alt = 0;
 
	if ( $comment_alt % 2 ) {
		if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
			$classes[] = 'odd-' . $user->user_login;
			$classes[] = 'alt-' . $user->user_login;
		} else {	
			$classes[] = 'odd';
			$classes[] = 'alt';
		}
	} else {
		if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
			$classes[] = 'even-' . $user->user_login;
		} else {
		$classes[] = 'even';
		}
	}
 
	$comment_alt++;
 
	// Alt for top-level comments
	if ( 1 == $comment_depth ) {
		if ( $comment_thread_alt % 2 ) {
			$classes[] = 'thread-odd';
			$classes[] = 'thread-alt';
		} else {
			$classes[] = 'thread-even';
		}
		$comment_thread_alt++;
	}
 
	$classes[] = "depth-$comment_depth";
 
	if ( !empty($class) ) {
		if ( !is_array( $class ) )
			$class = preg_split('#\s+#', $class);
		$classes = array_merge($classes, $class);
	}
 
	return apply_filters('comment_class', $classes, $class, $comment_id, $post_id);
}
评论 (8) 引用 (1) 发表评论 引用文章
  1. 2010年06月18日, 4:26 下午 | #1

    还是没搞懂作者设置的问题
    是换掉1、2还是怎样呢?

  2. 2010年01月4日, 12:20 下午 | #2
  3. 2010年01月4日, 11:03 上午 | #3

    @ray1980: 原作者是?

  4. 2009年08月14日, 7:46 下午 | #4

    原来如此

    多谢!

  5. 2009年06月27日, 9:03 上午 | #5

    @纪小年: 设计?我只会仿。

  6. 2009年06月27日, 8:37 上午 | #6

    老柯 看你分析这么多,给我们设计一个出来呗

  7. 2009年03月9日, 7:51 下午 | #7

    不好意思,原作者不同意。

  8. 2009年03月9日, 7:44 下午 | #8

    能不能做出来分享啊?

  1. 一月 6th, 2010, 01:40:24 | #1

发表回复

顶端
底端