WordPress.org

Ready to get started?Download WordPress

Forums

Ranked Title for Each Comment Autor (4 posts)

  1. Fernando Yet To Die
    Member
    Posted 3 years ago #

    Hi.

    I have added this function on my functions.php

    function commentCount() {
        global $wpdb;
        $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author = "' . get_comment_author() . '"');
        echo $count . '';
    }

    And this code <?php commentCount(); ?> to call forth the Total of Comment posted by each individual Visitor.

    The function above, searches for a "Name" used by a visitor when he posted his Comments, and so collects the total of comments for each unique "Name".

    All works great so far!

    Now... I want to create a "IF" conditions to Print a different "Title" depending on how many comments a certain Name has.

    To my knowledge the variable commentCount() has the value of the current comment name... So, I would use this on my If conditions? See below...

    <?php 
    
    if ($commentCount > 10) {
    	echo "Level 10"; }
    if ($commentCount > 1) {
    	echo "Level 01";
    }
    
    ?>

    What I am trying is. IF commentCount is bigger than 10, echo "Level 10". IF commentCount is bigger than 1, echo "Level 1".

    Can anyone help please?

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

    your function echos the count;
    however, for use in a comparitive if statement, you would need the function to return the count.

    possibly write a new function:

    function get_commentCount() {
        global $wpdb;
        $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author = "' . get_comment_author() . '"');
        return $count;
    }

    and:

    <?php 
    
    if (get_commentCount() > 10) {
    	echo "Level 10"; }
    if (get_commentCount() > 1) {
    	echo "Level 01";
    }
    
    ?>
  3. Fernando Yet To Die
    Member
    Posted 3 years ago #

    Omg, it works. You are a heavenly angel! (lol) Thank you!

    I am getting two echo's for a post higher than 1, any solution for this?

    I basically want to get:

    If 5 posts, return "Level 1".
    If 20 posts, return "Level 2".

    <?php 
    
    if (get_commentCount() < 5) {
    	echo "Level 1"; }
    
    	else 
    
    if (get_commentCount() < 10) {
    	echo "Level 2"; }
    
    	else
    
    if (get_commentCount() < 20) {
    	echo "Level 3"; }
    
    	else
    	echo "Level 10";
    
    ?>
  4. Fernando Yet To Die
    Member
    Posted 3 years ago #

    Cant believe its already working:

    <?php 
    
    if (get_commentCount() < 2) {
    	echo "1"; }
    	else
    if (get_commentCount() < 5) {
    	echo "2"; }
    	else
    if (get_commentCount() < 8) {
    	echo "3"; }
    	else
    if (get_commentCount() < 12) {
    	echo "4"; }
    	else
    if (get_commentCount() < 18) {
    	echo "5"; }
    	else
    if (get_commentCount() < 25) {
    	echo "6"; }
    	else
    if (get_commentCount() < 28) {
    	echo "7"; }
    	else
    if (get_commentCount() < 35) {
    	echo "8"; }
    	else
    if (get_commentCount() < 40) {
    	echo "9"; }
    	else
    if (get_commentCount() < 50) {
    	echo "10"; }
    	else
    if (get_commentCount() < 60) {
    	echo "15"; }
    	else
    if (get_commentCount() < 70) {
    	echo "20"; }
    	else
    if (get_commentCount() < 85) {
    	echo "25"; }
    	else
    if (get_commentCount() < 100) {
    	echo "30"; }
    	else
    if (get_commentCount() < 120) {
    	echo "35"; }
    	else
    if (get_commentCount() < 140) {
    	echo "40"; }
    	else
    if (get_commentCount() < 160) {
    	echo "45"; }
    	else
    if (get_commentCount() < 200) {
    	echo "50"; }
    	else
    if (get_commentCount() < 250) {
    	echo "55"; }
    
    	else
    	echo "60";
    
    ?>

    Any idea how to put this as a function, rendering the result in a $resulthere so I can call it <?php resulthere() ?> by just a line on my comments?

    Thou, I am happy already, this works perfect!!! YEYE!!

Topic Closed

This topic has been closed to new replies.

About this Topic