WordPress.org

Ready to get started?Download WordPress

Forums

Change comment style for Authors? (10 posts)

  1. warpdesign
    Member
    Posted 6 years ago #

    Hopefully this is an easy question, I would like comments made by authors or admins to have a different style than other comments. The part I'm having problems with is the PHP where it needs to evaluate if the comment is made by one or the other and then add the class accordingly. I'm not sure if there's something already built in to WordPress that allows you to change the class of the comment based on that criteria, but I think there probably is since it's very common on blogs to have that.

  2. syrupcore
    Member
    Posted 6 years ago #

  3. designsimply
    Member
    Posted 6 years ago #

    Matt Cutts posted a really good example for how to change the author comment style. See How to highlight author comments in WordPress on mattcutts.com.

    Edit: Doh! I didn't see that syrupcore already posted the same link when I added my reply. Sorry!

  4. Doodlebug
    Member
    Posted 5 years ago #

    Would this work on a multi-author blog? To highlight comments from the author in his/her own post?

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    The basic code you need to do something like this is as follows:

    $class = '';
    if ($comment->user_id == get_the_author_ID()) $class = 'bypostauthor';

    Then, you put your $class into the class of something around that comment. Like, maybe, the comment has a div surrounding it.

    Then the bypostauthor gets added only to comments made by the author of each post, and you can style accordingly.

    There's a reason I suggest using "bypostauthor" here, BTW. (2.7 hint!)

  6. jberghem
    Member
    Posted 5 years ago #

    Hey that's great. Couldn't you also do it by using the user ID as a class? If you have a <div> that contains the post and comments, then give the comment <div> the same class. You are then able to create a CSS selector that specifies a comment that has the same class as the containing <div> to be styled differently.

    For example:
    generated (X)HTML

    <div class="authorID">
        <div class="post>post content</div>
        <div class="authorID">comment from author</div>
      </div>

    CSS
    .authorID authorID { background-color: #CCC; }

    you'll just have to make a small edit to the wp-comments.php file to get the class in there.

    I just love the creative power that the combination of PHP, (X)HTML and CSS gives you. The ability to achieve the same thing visually by using completely different code, just gives you so much freedom.

    @Otto42 - thanks for your post, I learned something.

  7. Doodlebug
    Member
    Posted 5 years ago #

    Thanks guys but i'm getting some weird results, because i'm getting confused with adding this in with the alternative comment stuff that's already there.

    Here's what I have (lines 12, 24, 47 and 48 ): http://www.pastebin.ca/1205824

    What happens is a random comment poster (Dave, rather than fatrabbit - aka admin who happens to be the author of this post) gets the class added to the new div as seen in the source code here: http://www.werebunny.com/aa/2008/09/17/1-bit-audio-player-test-for-mp3s/

    I guess I don't really want an extra div just for this as it's harder to style in conjunction with alternative post styling. How can I integrate this into the alt comment styling on the
    < li >< /li > ?

  8. jberghem
    Member
    Posted 5 years ago #

    I would first make sure the generated (X)HTML is valid (you can check it at http://validator.w3.org) Having errors in your code can throw styling off. (more so than extra div's) I also saw you use a number as the ID. An ID may contain a number but is not allowed to start with a number.

    Take it from me that solving your CSS problems will be much easier when there are no errors in the (X)HTML. I've spent hours trying to fix a problem with CSS without success only to find a missing end tag in my XHTML which was causing the problem.

  9. Doodlebug
    Member
    Posted 5 years ago #

    I think the main reason why it doesn't validate is to do with either the 1 Pixel Out Audio Player plugin or the SoundCloud player (which uses <embed>) - both Flash entities.

    I've got it up on its own site now btw: http://www.abletonartists.com/

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    What you need to do is change the class thing to only include the class, not the whole class= stuff.

    Basically, make the LI into <li class='<?php echo $myclass;?>' ... and then set $myclass accordingly.

    For the alt stuff, make $myclass = '' or 'alt'. Then, for the author, add ' byauthor' to the $myclass.

Topic Closed

This topic has been closed to new replies.

About this Topic