WordPress.org

Ready to get started?Download WordPress

Forums

Controlling comments in layout based on status (5 posts)

  1. Danielx64
    Member
    Posted 1 year ago #

    Hello,

    I can use

    <?php if ($comment->comment_approved == '0') : ?>

    and

    <?php if ($comment->comment_approved == '1') : ?>

    to control umapproved and approved comments (layout) but I can't seen to do the same with

    <?php if ($comment->comment_approved == 'spam') : ?>.

    Can someone please explain why that would be?

    Thank-you :)

    PS, I would like to be able to use

    <?php if ($comment->comment_approved == 'trash') : ?> as well.

  2. Chris
    Member
    Posted 1 year ago #

    I don't know what your $comment object contains but generally speaking 'approved' is a binary state (either approved or not). Maybe you could try <?php if( $comment->comment_spam == 1 ) : ?> But this will depend on how your $comment object is structured.

    Or you could use this WP function http://codex.wordpress.org/Function_Reference/wp_get_comment_status to get a comment status, no matter what it is, and then check for 'deleted', 'approved', 'unapproved', 'spam'...

  3. bcworkz
    Member
    Posted 1 year ago #

    Before the comment gets inserted, the binary 1 or 0 is used by core code as Chris suggests, but the DB field type is varchar, so if you're working with values from the DB, the string version as you use is correct. Actually, the function wp_get_comment_status() uses code very similar to yours, so I'm puzzled why you're having trouble. I take it this code is on a template? Exactly how are you getting the $comment object assigned?

    If it's coming from a query, you might try === instead of == for comparisons. I don't know why, but it had made a difference for me in the past in relation to query results.

  4. Danielx64
    Member
    Posted 1 year ago #

    Hi guys,

    Thankyou for the answers :) Yes bcworkz (Sorry that I don't know your name :() I'm doing this in a template and my theme's function.php file.

    my comments.php file has

    <?php $args = array(
        'walker'            => null,
        'callback'          => 'phpbb_comments',
        'type'              => 'comment',
        'style'             => ''); ?>
        <?php wp_list_comments( $args); ?>

    And my functions.php file has http://pastebin.com/JmuGYx8B (posted on pastebin).

    Now the code works fine when I use <?php if ($status == 'approved') : ?> or <?php if ($status == 'unapproved') : ?> but nothing happens when I put in deleted or spam as the keyword.

    What I am trying to do here is bring some of the features from wp-admin and bring it out into the frontend area so that you can make changes to posts and comments without having to go into the backend (wp-admin).

  5. bcworkz
    Member
    Posted 1 year ago #

    Are you really getting unapproved comments in your callback? The usual query used for wp_list_comments() requires only approved comments be returned. Your callback shouldn't need to handle any type of comment besides approved.

    Unless you filtered the query arguments to return other types?

    (for all intents and purposes on the 'net my name is bcworkz, sometimes called bc or bcw in replies. But my real name is no big secret. Glenn)

Topic Closed

This topic has been closed to new replies.

About this Topic