• Resolved tlw22

    (@tlw22)


    Hi,

    I recently found out a way to display the amount of comments next to a users name. What I would like to do with this however is compile an if statement so that if a user has over xx amount of comments it displays an image next to their name. The only problem is I’m a php noob and don’t really have much of an idea on how to do this. Any help?

    My code so far (doesn’t work, but might help as a guidline?):

    <?php if ( author_comment_count() <= 3 ) : ?>
    // if a user has less than 3 comments display
    <img src="image 1 here" />
    <?php else: ?>
    // otherwise display
    <img src="image 2 here" />
    <?php endif; ?>

    The code to display the comments count is:

    author_comment_count();

    Live example, of what I’m trying to do http://chrs.biz/theme2/?p=44#comments

    Many thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    (@alchymyth)

    where is that function author_comment_count(); defined?
    does it echo the result or return it?

    for use in the conditional statement, you need a function that returns the value.

    Thread Starter tlw22

    (@tlw22)

    The author_comment_count(); is defined in the functions file:

    // Displays no. comments
    
    function author_comment_count(){
    	$oneText = '1';
    	$moreText = '%';
     	global $wpdb;
    	$result = $wpdb->get_var('
    		SELECT
    			COUNT(comment_ID)
    		FROM
    			'.$wpdb->comments.'
    		WHERE
    			comment_author_email = "'.get_comment_author_email().'"'
    	);
    	if($result == 1):
    		echo str_replace('%', $result, $oneText);
    	elseif($result > 1):
    		echo str_replace('%', $result, $moreText);
    	endif;
    }

    I’m not sure if the function is already returning the value, but if not, how can I get it to do so?

    Thanks!

    Michael

    (@alchymyth)

    one possibility:
    change the function to:

    function author_comment_count($echo=1){
    	$oneText = '1';
    	$moreText = '%';
     	global $wpdb;
    	$result = $wpdb->get_var('
    		SELECT
    			COUNT(comment_ID)
    		FROM
    			'.$wpdb->comments.'
    		WHERE
    			comment_author_email = "'.get_comment_author_email().'"'
    	);
    
    	if($echo==0) : return $result;
    
    	elseif($result == 1):
    		echo str_replace('%', $result, $oneText);
    	elseif($result > 1):
    		echo str_replace('%', $result, $moreText);
    	endif;
    }

    the difference is, that the function can now use one parameter, which changes from ‘echo’ to ‘return’ if set to 0 (zero) – left empty, it will echo the result, if set to 0, it will return the result.

    this should make no difference in the areas where you output the author comment count.

    and now in your conditional statement, use it this way:

    <?php if ( author_comment_count(0) <= 3 ) : ?>
    // if a user has less than 3 comments display
    <img src="image 1 here" />
    <?php else: ?>
    // otherwise display
    <img src="image 2 here" />
    <?php endif; ?>
    Thread Starter tlw22

    (@tlw22)

    Thanks, I will give it a go!

    Thread Starter tlw22

    (@tlw22)

    Yep, works perfectly. Thanks for all your help, much appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Counting Comments’ is closed to new replies.