IamTrent
IamTrent

Reputation: 395

Wordpress conditionally load class when comments author matches author of the post

I want to be able to conditionally load a class containing the word 'AUTHOR' in the comments section, next to the name of the person who was the author of the post. I know it has something to do with class bypostauthor, what I've got so far is:

<?php if $class == 'bypostauthor' ?>
    <div class="author">AUTHOR</div> 
<? endif; ?>

More info:

In comment section of my wordpress post page, the class .bypostauthor exists whenever a the author of post page comments on there post page, giving this for one of the comments in the comment section ->

<li class="comment byuser comment-author-admin bypostauthor odd alt thread-odd thread-alt depth-1 clearfix" id="li-comment-28">

        <div class="comment-block" id="comment-28">
            <div class="comment-inside-block">
            <div class="comment-info">  
                <div class="comment-author vcard clearfix">

                    <img alt="" src="http://0.gravatar.com/avatar/6dbfd7e6bd95881c5736647c8736dd89?s=32&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&amp;r=G" class="avatar avatar-32 photo" height="32" width="32">                  
                    <div class="comment-meta commentmetadata">
                        <cite class="fn"><a href="http://trentaughey.com" rel="external nofollow" class="url">Trent Aughey</a></cite>                       
                        <div style="clear:both;"></div>
                        <a class="comment-time" href="http://trentaughey.com/test/#comment-28">  1 day ago   </a>                   </div>
                </div>
            <div class="clearfix"></div>
            </div>

            <div class="comment-text">
                <p>comment – 2</p>
                <p class="reply">
                    <a class="comment-reply-link" href="/test/?replytocom=28#respond" onclick="return addComment.moveForm(&quot;comment-28&quot;, &quot;28&quot;, &quot;respond&quot;, &quot;570&quot;)">Reply</a>              </p>
            </div>
            </div>


        </div>
<ul class="children">
    <li class="comment byuser comment-author-admin bypostauthor even depth-2 clearfix" id="li-comment-29">

        <div class="comment-block" id="comment-29">
            <div class="comment-inside-block">
            <div class="comment-info">  
                <div class="comment-author vcard clearfix">

                    <img alt="" src="http://0.gravatar.com/avatar/6dbfd7e6bd95881c5736647c8736dd89?s=32&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&amp;r=G" class="avatar avatar-32 photo" height="32" width="32">                  
                    <div class="comment-meta commentmetadata">
                        <cite class="fn"><a href="http://trentaughey.com" rel="external nofollow" class="url">Trent Aughey</a></cite>                       
                        <div style="clear:both;"></div>
                        <a class="comment-time" href="http://trentaughey.com/test/#comment-29">  1 day ago   </a>                   </div>
                </div>
            <div class="clearfix"></div>
            </div>

            <div class="comment-text">
                <p>HELLO</p>
                <p class="reply">
                                    </p>
            </div>
            </div>


        </div>

When a non-author of the post page leaves a comment on the post the following code is used

<li class="comment even thread-even depth-1 clearfix" id="li-comment-20">

        <div class="comment-block" id="comment-20">
            <div class="comment-inside-block">
            <div class="comment-info">  
                <div class="comment-author vcard clearfix">

                    <img alt="" src="http://0.gravatar.com/avatar/6dbfd7e6bd95881c5736647c8736dd89?s=32&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&amp;r=G" class="avatar avatar-32 photo" height="32" width="32">                  
                    <div class="comment-meta commentmetadata">
                        <cite class="fn"><a href="http://trentaughey.com/" rel="external nofollow" class="url">Trent Aughey</a></cite>                      
                        <div style="clear:both;"></div>
                        <a class="comment-time" href="http://trentaughey.com/test/#comment-20">  1 month ago   </a>                 </div>
                </div>
            <div class="clearfix"></div>
            </div>

            <div class="comment-text">
                <p>Test, Test</p>
                <p class="reply">
                    <a class="comment-reply-link" href="/test/?replytocom=20#respond" onclick="return addComment.moveForm(&quot;comment-20&quot;, &quot;20&quot;, &quot;respond&quot;, &quot;570&quot;)">Reply</a>              </p>
            </div>
            </div>


        </div>

I'm trying to accomplish having the word 'AUTHOR' next to the author of post name whenever they comment on there page. Considering bypostauthor class appears whenever page author post a comment on his page I tried to use the following code to make a div class conditionally appear whenever the post author comments ->

<li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">

    <div class="comment-block" id="comment-<?php comment_ID(); ?>">
        <div class="comment-inside-block">
        <div class="comment-info">  
            <div class="comment-author vcard clearfix">

                <?php echo get_avatar( $comment->comment_author_email, 32 ); ?>

                <div class="comment-meta commentmetadata">
                    <?php printf(__('<cite class="fn">%s</cite>', 'playne'), get_comment_author_link()) ?><?php if $class == 'bypostauthor' ?>
    <div class="author">AUTHOR</div> 
<? endif; ?>

                    <div style="clear:both;"></div>
                    <a class="comment-time" href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>">  <?php echo themeblvd_time_ago_1(); ?>   </a><?php edit_comment_link(__('(Edit)', 'playne'),'  ','') ?>
                </div>
            </div>
        <div class="clearfix"></div>
        </div>

        <div class="comment-text">
            <?php comment_text() ?>
            <p class="reply">
                <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
            </p>
        </div>
        </div>
        <?php if ($comment->comment_approved == '0') : ?>
            <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'playne') ?></em>
        <?php endif; ?>    

    </div>

My code fix does nothing, can anyone tell me what I'm doing wrong please.

Upvotes: 1

Views: 574

Answers (2)

Patrick
Patrick

Reputation: 21

You can filter the comment author name instead of adding something to core files. Your change will be overwritten on a WordPress update.

add_filter( 'get_comment_author', 'your_prefix_filter_the_post_author_name' );

function your_prefix_filter_the_post_author_name( $author ) {
$classes = get_comment_class();
    if (in_array( 'bypostauthor',$classes) ) {
       $output = '<div class="author-tag"><p>AUTHOR - ' . $author . '</p></div>';
    return $output;
    }
    return $author;
}

Upvotes: 2

IamTrent
IamTrent

Reputation: 395

I was able to fix this problem using the following code:

function author_tag() {
$classes = get_comment_class();
if(in_array('bypostauthor',$classes)) {
    $output = '<div class="author-tag"><p>AUTHOR</p></div>';
} /*else {
    $output = '<div class="author-tag"><p>NOT AUTHOR</p></div>';
}*/
return $output;}

Placed the function as follows:

<li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">

    <div class="comment-block" id="comment-<?php comment_ID(); ?>">
        <div class="comment-inside-block">
        <div class="comment-info">  
            <div class="comment-author vcard clearfix">

                <?php echo get_avatar( $comment->comment_author_email, 32 ); ?>

                <div class="comment-meta commentmetadata">
                    <?php printf(__('<cite class="fn">%s</cite>', 'playne'), get_comment_author_link()) ?>

                    <?php echo author_tag(); ?>

                    <div style="clear:both;"></div>
                    <a class="comment-time" href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ) ?>">  <?php echo themeblvd_time_ago_1(); ?>   </a><?php edit_comment_link(__('(Edit)', 'playne'),'  ','') ?>
                </div>
            </div>
        <div class="clearfix"></div>
        </div>

        <div class="comment-text">
            <?php comment_text() ?>
            <p class="reply">
                <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
            </p>
        </div>
        </div>
        <?php if ($comment->comment_approved == '0') : ?>
            <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'playne') ?></em>
        <?php endif; ?>    

    </div>

Hope this helps anyone who had similar issues.

Upvotes: 1

Related Questions