最近はテスト環境でコメント欄をずっとカスタマイズしてるんですが、私の使っているWordPressのテーマのコメントファイル「comments.php」を覗いてみると、
<?php comment_class('clearfix'); ?>という記述がありました。「これってなんだろな~」と思ったので、ちょっとだけですが調べました。
「comment_class」を使えば任意のクラス名を付与もできる
Codex「Function Reference/comment class」を参考にさせて頂いて、コード例を書きました。
例1:comment_class()と書く場合
【コード】 <li <?php comment_class(); ?>> 【結果】 <li class="comment byuser comment-author-admin bypostauthor even thread-even depth-1">
クラス名が付与されました。このクラス名の付与ルールは、Codex「Function Reference/comment class」に書かれています。(英文です)
・・・今回上に挙げた例ですと、コメントの階層「depth-1」や、スレッド「thred-even」、コメントをしたユーザ「comment-author-admin」など、クラス名が割り振られていますね。(もっと厳密な説明ができるとは思いますが、このくらいで・・・)
例えば、コメントに「管理者じゃないユーザが」「返信」という形でコメントをすると、
<li class="comment even depth-2">
って感じでクラス名が付与されます。
例2:comment_class(‘clearfix’)と書く場合
【コード】
<li <?php comment_class('clearfix'); ?>>
【結果】
<li class="comment byuser comment-author-admin bypostauthor even thread-even depth-1 clearfix">任意の文字列をクラス名として追加できるようです。
ちなみにですが、「comment_class(‘clearfix’)」と書いた場合も、コメントに「管理者じゃないユーザーが」「返信」すると、
<li class="comment even depth-2 clearfix">
って感じでクラス名が付与されます。clearfixは固定ですね。コメント全体に共通したクラス名を付与したい場合にはこれを使うといい感じですね。
というわけで、コメント欄のカスタマイズって今まであんまりやったことがなかったので、知らないことが多いです。とても楽しく勉強しながら着々と出来上がってます~。
「comment_class」の中身
「英文がわからなくてもコードを見ればなんとなくわかる!」という場合も私は往々にしてあります。というわけで「comment_class」の処理を見ていると、なんとなく何をやっているのか、クラス名の付与される条件分岐のタイミングなどもわかるんで、なるほど~って感じですね。
comment_classの中身(WordPressのバージョン3.5.1の場合)
function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
// Separates classes with a single space, collates classes for comment DIV
$class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
if ( $echo)
echo $class;
else
return $class;
}それで、上の太字部分の「get_comment_class」はどんな処理をしているのか見ましょう。
get_comment_classの中身(WordPressのバージョン3.5.1の場合)
function get_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';
$classes[] = 'comment-author-' . sanitize_html_class($user->user_nicename, $comment->user_id);
// 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 ) {
$classes[] = 'odd';
$classes[] = 'alt';
} 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);
}
$classes = array_map('esc_attr', $classes);
return apply_filters('comment_class', $classes, $class, $comment_id, $post_id);
}とりあえずですが、自分的には理解できました。何かのご参考となりましたら幸いです。
コード表示が見やすいとうれしいです!
おうえんしてます!
2013/06/07 22:25:40
ツイート
シェア
ちょっといいやつ実装してみます!ありがとうございます!
2013/06/07 23:08:09
ツイート
シェア