0

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 ->

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>
"> 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>

Tried something based on answer ->

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;
}
1

You were nearly there with your first piece of code. For this to completely work, you'll need to get a list of the comment classes and check your class against that list. If your class exists in that returned list, you can something, if not, do something else

Use get_comment_class() to retrieve that list. Here is an example

$classes = get_comment_class();
if(in_array('bypostauthor',$classes)) {
    // do something for bypostauthor class
} else {
    // do something else if bypostauthor class don't exist
}

EDIT 2

Your problem is pure syntax. Your HTML needs to be between single quotes ('), otherwise it is read as php, which is invalid php, caussing the syntax error. Check my example below

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;
}

I would advice you to get a good syntax highligher, this will help you a lot with invalid syntaxes

  • possibly use get_comment_class() – Michael Aug 11 '14 at 10:45
  • @Micheal yes, thanks for that. Blue Monday. Will update my answer – Pieter Goosen Aug 11 '14 at 11:11
  • @PieterGoosen Ok, I'm really new to php but my attempt at it looks like this -> 'code'$classes = get_body_class(); if(in_array('bypostauthor',$classes)) { <div class="author-tag"><p>AUTHOR</p></div> } else { <div class="not-author-tag"><p>NOT AUTHOR</p></div> } – IamTrent Aug 11 '14 at 11:43
  • Ok, ya, you have a problem with syntax here – Pieter Goosen Aug 11 '14 at 11:56
  • @PieterGoosen I updated the main post with the new code I used, I get the error -> Parse error: syntax error, unexpected '<' in /home/trentaug/public_html/wp-content/themes/Typer/functions.php on line 10 – IamTrent Aug 11 '14 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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