Forums

Highlight comments from ALL site authors (2 posts)

  1. danphilibin
    Member
    Posted 2 months ago #

    I've spent a good while toying with PHP and doing some Googling but I have yet to find a solution: How can I add a CSS class to comments from ALL of the site's authors?

    There's countless tutorials on how to highlight the post author's comments, but if another contributor from the site adds their thoughts, how can that comment be highlighted too?

    Here's my if() statement:

    <?php if($post->post_author == $comment->user_id || in_array($comment->comment_author, $names)) { ?>

    $names is supposed to be an array of the display names of every author on the site, but nothing I've tried has been able to create a correct array of those display names.

    Maybe the array setup isn't the right way to do this, I don't know. I could go the route of IDs instead of display names but on this particular site, some of the authors created a duplicate user of themselves by accident and weren't always logged into their author account when commenting.

    Any tips would be appreciated!

    Dan

  2. t31os_
    Member
    Posted 2 months ago #

    Here's something to play with...

    $usercaps = get_usermeta($comment->user_id,'wp_capabilities');
    if($usercaps){
    	// It's a logged in user - $usercaps is an array
    	foreach($usercaps as $role => $unwanted) {
    		unset($unwanted);
    		$capabilities = $role; // Set this for each result, there should never be more then 1 result ..
    	}
    }
    else {
    	// User not logged in - visitor
    	$capabilities = '';
    }
    print $capabilities;

    Obviously move the print part into the element and it'll print out the role of the user who has commented.. (unless they're a vistor)..

    Modify as required..

Reply

You must log in to post.

About this Topic