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);
}