Quantcast
Channel: I Have the Conch » wordpress
Viewing all articles
Browse latest Browse all 2

WordPress… Y U No Love Me?

$
0
0

OK, so I’m trying to release this as a theme, but here’s the problem.

Currently, comment_class() add “odd” or “even” to a comment’s class, presumably to enable alternating colors on a list of comments. It’s pretty fail when it comes to threaded comments, though.

  • comment 1
    • first reply to comment 1
      • reply to the reply
    • Second reply to comment 1
  • comment 2

In this example, clearly you’d want comment 1 to be class=”odd” and comment 2 to be class=”even” if you were alternating colors. No dice. comment_class is no respecter of hierarchy or depth.

In this scenario you get:

  • comment 1: class=”odd”
    • first reply to comment 1: class=”even”
      • reply to the reply : class=”odd”
    • Second reply to comment 1: class=”even”
  • comment 2: class=”odd”

Note that comment 1 and comment 2 both get the same class, and therefore if they’re used for styles, they’ll get the same appearance. True, they also get “thread-odd” and “thread-even” (respectively) so you can style on that, but that doesn’t help with first reply and second reply — which both get class=”even”. Only the first level in the hierarchy gets “thread-odd” and “thread-even” so you’ve still got two sequential comments with identical styling.

It would be useful if the “odd” and “even” assignments reset for each level of a thread. Then I could use them in conjunction with their level (which is already assigned by comment_class()) to get approprite styles. So we’d get this:

  • comment 1 (class=”odd thread-odd depth-1″)
    • first reply to comment 1 (class=”odd depth-2″)
      • reply to the reply (class=”odd depth-3″)
    • Second reply to comment 1 (class=”even depth-2″)
  • comment 2 (class=”even thread-even depth-1″)
    • first reply to comment2 (class=”odd depth-2″)

if a depth was an even number, it would have one pattern for odd/even. If the depth was an odd number, then it would have a different pattern for odd/even.

I have had a number of thoughts today for how to fix this so I could get what I wanted. Using jQuery to correct all the classes after it all loaded was one idea. Another was to write my own functions for not only how each comment displays but also for displaying the whole list. In the end, I think the simplest way is to hack the core modules, which means it won’t be going into the distributed version of this theme. Although I might find a simple way to get it all in how I want it without duplicating the entire comment_function.php file.

Anyway, for the curious, this is how I do it. I use the following code to replace WordPress’s get_comment_class() in */includes/comment-template.php

function get_comment_class( $class = ”, $comment_id = null, $post_id = null ) {
  global $comment_alt, $comment_depth, $comment_thread_alt, $mdg_leveltrack, $mdg_lastdepth;
  if (!empty($comment_depth) ) $mdg_lastdepth = $comment_depth; #eric did this
  $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;
  #Eric’s code starts
  if ( empty($mdg_leveltrack) ) $mdg_leveltrack= array(1=>0, 0,0,0,0,0,0,0,0,0);
  if ( empty($mdg_lastdepth) ) $mdg_lastdepth = 0;
  if ($mdg_lastdepth < $comment_depth) $mdg_leveltrack[$comment_depth] = 0;
  $mdg_leveltrack[$comment_depth] ++;
  if ($mdg_leveltrack[$comment_depth] % 2) {
    $classes[] = ‘odd';
    $classes[] = ‘alt';
  } else {
    $class[] = ‘even';
  }
  /* end eric’s code, but then he commented out this bit here
  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);
}

Viewing all articles
Browse latest Browse all 2

Trending Articles