Opened 3 years ago

Last modified 3 years ago

#12223 new feature request

Adding a titles to comment levels with the walker class

Reported by: danielpataki Owned by:
Priority: normal Milestone: Future Release
Component: Comments Version:
Severity: normal Keywords: walker class, wp_list_comments
Cc:

Description

Hi Everyone!

This is only my second post here, and my first feature request, so please excuse me if I have duplicated something or my solution is completely unsuitable.

I am experimenting with the CMS capabilities of Wordpress, especially the enhanced post_type GUI in the new 3.0 alpha release. While building a sort of Yahoo Answers type platform, I wanted to have 1st level comments as answers to a question (a post), and 2nd level comments as comments on the answer (1st level comments). I wanted to label comments on an answer as "comments", but this is not really possible to do, to my knowledge, using "wp_list_comments".

I created my own function for wp_list_comments so I didn't have to modify Wordpress core, in order to get at the walker class at the bottom of wp_list_comments. I wanted to use start_lvl to add a title for comment level 2. However, start_lvl in "class Walker_Comment extends Walker" will only let you change the list type to "ul", "ol" or "div". My idea is to add an argument which would allow you to add a title, along with specifying it's heading (or surrounding tag). As I said, I am not an expert on Wordpress coding standards, so the following code may be completely off.

/**
   * @see Walker::start_lvl()
   * @since unknown
   *
   * @param string $output Passed by reference. Used to append additional content.
   * @param int $depth Depth of comment.
   * @param array $args Uses 'style' argument for type of HTML list.
   * @param array $args Uses 'title' argument for title text.
   * @param array $args Uses 'title_heading' argument for title text heading level.
   */
	

function start_lvl(&$output, $depth, $args) {
  $GLOBALS['comment_depth'] = $depth + 1;
  
    switch ( $args['style'] ) {
      case 'div':
        break;
      case 'ol':
        echo "<ol class='children'>\n";
        break;
     default:
     case 'ul':
        echo "<ul class='children'>\n";
        break;
    }
	
	if (!empty ($args['title'])) {
	  
      if(!empty ($args['title_heading'])) {
	    echo '<'.$args['title_heading'].'>';
	  }
	  
	  echo $args['title'];

	  if(!empty ($args['title_heading'])) {
	    echo '</'.$args['title_heading'].'>';
	  }
	
	}
	
}

Change History (3)

You can provide wp_list_comments() with a callback. You can also extend the existing walker if you need that kind of control.

  • Milestone changed from Unassigned to Future Release
  • Type changed from enhancement to feature request

Yes, I normally use a callback to format everything, but while this allows me to format any comment, I cant do anything before the 2nd level comments start for a first level.

I just got into objects in PHP a month or so, and I don't have the expertise to do that quickly for myself, so I will probably just use a custom query to list all the comments I need, and their sub-comments, adding a title before the 2nd level ones.

It would however be an option which would feel very "wordpress"-like, to enable comment level titles with a "title_li", perhaps in the wp_list_comments itself. This would be a little more difficult since you can have many levels, so maybe an array would be best?

Anyway, thanks for helping out. I will try and submit better tickets, please do tell me if I am being over-helpful here.

Note: See TracTickets for help on using tickets.