Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am fairly new to Wordpress and I am currently designing a template for a client of our company. However, I need to assign a specific id to the last comment of a post.

Basically I have my own function that is called on the callback of wp_list_comments(). this is due to the fact that I have a very customised comment layout.

Now, in the functions.php I would like to assign simply with an if statement such as

if ( is_last_comment() ) : print "last-comment" endif; 

I have looked here and in the codex and on other pages and I cannot find anything.

I appreciate anykind of help.

Thank you very much in advance.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

I guess you use the comments loop(?). If so, you could

Possibility A

count the comments first, set $i = 0; before the loop and $i++ at the beginning/inside the loop and then attach your needed id when `if ( $count < $i ) $css_id = "id='last-comment'".

Possibility B

You could take the comments object(?) and use $last_comment = count( $comments_obj ); and after the loop echo $comments->$last_comment;. If it's an array, you should use echo $comments[ $last_comment ]; of course.


Not sure which one works as I haven't dealt with comments for a while, but it should get you on the right track.

share|improve this answer
    
Thank you for your answer kaiser, I do unterstand what your are saying, but it seems pretty much impossible with the already implemented way how I did it. To show you my code: pastebin.com/Yn0dWxQA I only get the comment that is currently processed by <?php wp_list_comments( array ( 'callback' => 'ipa_comment_template' ) );?> Well, I dont certainly know that, but that is my explaination of it. As a count($comment) in my ipa_comment_template function always returns a 1. I suppose that I need to rethink of the solution that I have now and propably do it without the functions.php –  Richard Sep 1 '11 at 15:38
    
@Richard: No, you seem to be on the right track. The only Q is where to get the full comments from. I guess $GLOBALS['wp_query']->comments will do it. Maybe you can use a static $i = 0; before the loop and then try to count upwards inside your callback with $i++; or maybe by reference with &$i++;. Give it a try, unmark this A as solution and let's see where we get from here. –  kaiser Sep 1 '11 at 18:04
    
Btw: Have you already done a var_dump() of the $comment inside your callback? Maybe there's some counter hidden inside the comment object. –  kaiser Sep 1 '11 at 18:06
add comment

I'm sure there's a way to do it in the comments walker, but if I were approaching the problem I would probably do something like this:

Add a hook on saving a new comment that updates a "latest comment" flag on its parent post;

add_action( 'wp_insert_comment', 'set_last_comment', 10, 2 );

function set_last_comment_flag( $id, $comment ) {
    update_post_meta( $comment->comment_post_ID, '_last_comment', $id );
}

and then, use that flag to filter the comment classes.

add_filter( 'comment_class', 'flag_last_comment', 10, 4)

function flag_last_comment( $classes, $class, $comment_id, $post_id ) {
    if ( get_post_meta( $post_id, '_last_comment', true ) == $comment_id ) 
        $classes[] = 'last_comment';

    return classes;
}

Now, this flags the last comment written, not necessarily the bottom one as displayed on the page (you could have threaded comments, or multiple page of comments on a post). So I don't know how well it would work for you.

If you have to target the last comment displayed on the page, I would really just use javascript to apply whatever class you want to it. The logic required to figure that out from the comment walker would probably be a lot more complex than a single line of jQuery to go back after the page has rendered and find the last list item.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.