Make WordPress Core

Changeset 8695


Ignore:
Timestamp:
08/20/2008 11:48:09 PM (18 years ago)
Author:
ryan
Message:

comment_class(). Props sandbox theme. see #7560

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-content/themes/classic/comments.php

    r7361 r8695  
    1313
    1414<?php foreach ($comments as $comment) : ?>
    15         <li id="comment-<?php comment_ID() ?>">
     15        <li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
    1616        <?php echo get_avatar( $comment, 32 ); ?>
    1717        <?php comment_text() ?>
  • trunk/wp-content/themes/default/comments.php

    r7888 r8695  
    1515
    1616        /* This variable is for alternating comment background */
    17         $oddcomment = 'class="alt" ';
     17        $oddcomment = 'alt';
    1818?>
    1919
    … …  
    2727        <?php foreach ($comments as $comment) : ?>
    2828
    29                 <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
     29                <li <?php comment_class($oddcomment) ?>id="comment-<?php comment_ID() ?>">
    3030                        <?php echo get_avatar( $comment, 32 ); ?>
    3131                        <cite><?php comment_author_link() ?></cite> Says:
    … …  
    4343        <?php
    4444                /* Changes every other comment to a different class */
    45                 $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
     45                $oddcomment = ( empty( $oddcomment ) ) ? 'alt' : '';
    4646        ?>
    4747
  • trunk/wp-includes/comment-template.php

    r8600 r8695  
    217217function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
    218218        echo get_comment_author_url_link( $linktext, $before, $after );
     219}
     220
     221/**
     222 * Generates semantic classes for each comment element
     223 *
     224 * @since 2.7
     225 *
     226 * @param string|array $class One or more classes to add to the class list
     227 * @param int $comment_id An optional comment ID
     228 * @param int $post_id An optional post ID
     229 */
     230function comment_class( $class = '', $comment_id = null, $post_id = null ) {
     231        // Separates classes with a single space, collates classes for post DIV
     232        echo 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
     233}
     234
     235/**
     236 * Returns the classes for the comment div as an array
     237 *
     238 * @since 2.7
     239 *
     240 * @param string|array $class One or more classes to add to the class list
     241 * @param int $comment_id An optional comment ID
     242 * @param int $post_id An optional post ID
     243 * @return array Array of classes
     244 */
     245function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
     246        static $comment_alt;
     247
     248        $comment = get_comment($comment_id);
     249
     250        $classes = array();
     251
     252        // Get the comment type (comment, trackback),
     253        $classes[] = $comment->comment_type;
     254
     255        // If the comment author has an id (registered), then print the log in name
     256        if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
     257                // For all registered users, 'byuser'
     258                $classes[] = 'byuser comment-author-' . $user->user_nicename;
     259                // For comment authors who are the author of the post
     260                if ( $post = get_post($post_id) ) {
     261                        if ( $comment->user_id === $post->post_author )
     262                                $classes[] = 'bypostauthor';
     263                }
     264        }
     265
     266        if ( empty($comment_alt) )
     267                $comment_alt = 0;
     268
     269        if ( $comment_alt % 2 )
     270                $classes[] = 'odd';
     271        else
     272                $classes[] = 'even';
     273
     274        $comment_alt++;
     275
     276        if ( !empty($class) ) {
     277                if ( !is_array( $class ) )
     278                        $class = preg_split('#\s+#', $class);
     279                $classes = array_merge($classes, $class);
     280        }
     281
     282        return apply_filters('comment_class', $classes, $class, $comment_id, $post_id);
    219283}
    220284
Note: See TracChangeset for help on using the changeset viewer.