edow
Forum Replies Created
-
Forum: Hacks
In reply to: Remove text field from comment formYour instructions were very clear! I’m also glad it worked out as I wanted. Thanks!
Forum: Hacks
In reply to: Remove text field from comment formYou’re very helpful and I appreciate all the time you spent to help me. Many thanks! I think I’ve got it now.
I was following your step-by-step advise and before I was by the last step it was working already.
My function now is:
function aigoo_get_score() { $comments = get_comments( array('post_id' => get_the_ID(),) ); $likes = array(); $unlikes = array(); foreach( $comments as $comment ) { if('unlike' == $comment->comment_content ) $unlikes[] = $comment; else $likes[] = $comment; } $likescore = count( $likes ); $unlikescore = count( $unlikes ); $score = count($likes) - count($unlikes); $totalvotes = count($likes) + count($unlikes); return compact('likes', 'unlikes', 'likescore', 'unlikescore', 'score', 'totalvotes'); }I compacted the like and unlike arrays and also the score and totalvotes counts.
In single.php I call the function with:
$counts = aigoo_get_score(); extract( $counts );and than echo $score and $totalvotes for the counts.
To show the avatars I didn’t need to modify the code. This is the code I use:
echo '<div class="upvoters" id="votes">'; echo '<div class="upvoter-img">'; foreach( $likes as $like ) echo("<img src=\"".get_avatar_url(get_avatar( $like->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$like->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; echo '<div class="downvoters">'; echo '<div class="downvoter-img">'; foreach( $unlikes as $unlike ) echo("<img src=\"".get_avatar_url(get_avatar( $unlike->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$unlike->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>';English and coding 😉 are not my native languages, but hopefully I followed your steps properly. It looks like it’s working well. I guess when it’s working it’s alright? I don’t want problems later on, so if you (or someone else of course) foresees problems I’ll do my best to improve this code.
If the code is fine I would to thank you very much for teaching me all this! I’ve learned a lot with all your help!
Forum: Hacks
In reply to: Remove text field from comment form…to be clear, if I use the above code in the function itself it is not showing on the right place. So I need it hard coded in single.php I guess.
Forum: Hacks
In reply to: Remove text field from comment formThe avatars are only shown when I use this code in the function itself, but not when I hard coded it on single.php:
echo '<div class="upvoters" id="votes">'; echo '<div class="upvoter-img">'; foreach( $likes as $like ) echo("<img src=\"".get_avatar_url(get_avatar( $like->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$like->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; echo '<div class="downvoters">'; echo '<div class="downvoter-img">'; foreach( $unlikes as $unlike ) echo("<img src=\"".get_avatar_url(get_avatar( $unlike->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$unlike->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>';I only need it on single.php, because for example on category.php I only show to score and not the avatars. The rest is working great though. Awesome!
Forum: Hacks
In reply to: Remove text field from comment formI tried the following code on top of the single.php page:
$counts = aigoo_get_score(); extract( $counts ); //restores compacted variables $score = $likescore - $unlikescore; $totalvotes = $likescore + $unlikescore; if ($score > 0 ) { echo '<span class="plusscore">+'.$score.'</span><br>'; } elseif ($score == 0 ) { echo '<span class="zeroscore">'.$score.'</span><br>'; } elseif ($score < 0 ) { echo '<span class="minusscore">'.$score.'</span><br>'; } if ($totalvotes == 1 ) { echo '<span class="totalvotes">Score uit <a href="#comments">'.$totalvotes.' stem</a></span>'; } elseif ($totalvotes == -1 ) { echo '<span class="totalvotes">Score uit <a href="#comments">'.$totalvotes.' stem</a></span>'; } elseif ($totalvotes == 0 ) { echo '<span class="totalvotes"><a href="#comments">Nu stemmen!</a></span>'; } else { echo '<span class="totalvotes">Score uit <a href="#comments">'.$totalvotes.' stemmen</a></span>'; }And this where the avatars should be on single.php:
echo '<div class="upvoters" id="votes">'; echo '<span class="numberoflikes">('.count($likes).')</span>'; echo '<div class="upvoter-img">'; foreach( $likes as $like ) echo("<img src=\"".get_avatar_url(get_avatar( $like->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$like->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; echo '<div class="downvoters">'; echo '<span class="numberofunlikes">('.count($unlikes).')</span>'; echo '<div class="downvoter-img">'; foreach( $unlikes as $unlike ) echo("<img src=\"".get_avatar_url(get_avatar( $unlike->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$unlike->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>';And this in functions.php:
/* Display likes/unlikes */ function aigoo_get_score() { $comments = get_comments( array('post_id' => get_the_ID(),) ); $likes = array(); $unlikes = array(); foreach( $comments as $comment ) { if('unlike' == $comment->comment_content ) $unlikes[] = $comment; else $likes[] = $comment; } $likescore = count( $likes ); $unlikescore = count( $unlikes ); return compact('likescore', 'unlikescore'); }Now, the code on top of the page is working correctly, but there’re no avatars shown at the bottom of the page.
My idea was to remove the avatars code from the function and put it on the single.php page itself, so then I could call the function on top of the page.This is the code I use on for example the category page and it looks like it works correctly:
$counts = aigoo_get_score(); extract( $counts ); //restores compacted variables $score = $likescore - $unlikescore; $totalvotes = $likescore + $unlikescore; if ($score > 0 ) { echo '<span>Score +'.$score.'</span>'; } elseif ($score == 0 ) { echo '<span>Score '.$score.'</span>'; } elseif ($score < 0 ) { echo '<span>Score '.$score.'</span>'; } if ($totalvotes == 1 ) { echo '<span> uit '.$totalvotes.' stem</span>'; } elseif ($totalvotes == -1 ) { echo '<span> uit '.$totalvotes.' stem</span>'; } elseif ($totalvotes == 0 ) { echo '<span>Nu stemmen!</span>'; } else { echo '<span> uit '.$totalvotes.' stemmen</span>'; }So, right now I only need the avatars to show. Further, I’m very very happy with this function!
Forum: Hacks
In reply to: Remove text field from comment formTo be complete, this is my function code right now:
function aigoo_get_score() { $comments = get_comments( array('post_id' => get_the_ID(),) ); $likes = array(); $unlikes = array(); foreach( $comments as $comment ) { if('unlike' == $comment->comment_content ) $unlikes[] = $comment; else $likes[] = $comment; } echo '<div class="upvoters" id="votes">'; echo '<span class="numberoflikes">('.count($likes).')</span>'; echo '<div class="upvoter-img">'; foreach( $likes as $like ) echo("<img src=\"".get_avatar_url(get_avatar( $like->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$like->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; echo '<div class="downvoters">'; echo '<span class="numberofunlikes">('.count($unlikes).')</span>'; echo '<div class="downvoter-img">'; foreach( $unlikes as $unlike ) echo("<img src=\"".get_avatar_url(get_avatar( $unlike->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$unlike->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; $likescore = count( $likes ); $unlikescore = count( $unlikes ); return compact('likescore', 'unlikescore'); }Forum: Hacks
In reply to: Remove text field from comment formI’m sorry, your code is working *shame on me*.
But only after I call the function with:
$counts = aigoo_get_score();
This code shows the avatars indeed. But the extract code (below) have to work on top of the page, instead of only after the function call. Is that possible? If I call the function on top of the page, also the avatars are shown at the top of the page and that’s what I don’t want.Also I would like to show the extract code and the
$totalvotescode on the category, search and tag pages. Or do I have to call the function otherwise?So this is the extract code (I style it later, but it works):
extract( $counts ); //restores compacted variables echo "Number of Likes: $likescore\n"; echo "Number of Unlikes: $unlikescore\n";And this is the working $totalvotes code:
$totalvotes = $likescore + $unlikescore; echo ''.$totalvotes.'';So, my question now is how to show these codes on top of the page and on the category, search and tag pages.
Forum: Hacks
In reply to: Remove text field from comment formThe word “array” is now gone, because I forgot to use
$counts = aigoo_get_score();, but the scores are not showing yet.Forum: Hacks
In reply to: Remove text field from comment formI tried to following code in functions.php:
function aigoo_get_score() { $comments = get_comments( array('post_id' => get_the_ID(),) ); $likes = array(); $unlikes = array(); foreach( $comments as $comment ) { if('unlike' == $comment->comment_content ) $unlikes[] = $comment; else $likes[] = $comment; } echo '<div class="upvoters" id="votes">'; echo '<span class="numberoflikes">('.count($likes).')</span>'; echo '<div class="upvoter-img">'; foreach( $likes as $like ) echo("<img src=\"".get_avatar_url(get_avatar( $like->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$like->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; echo '<div class="downvoters">'; echo '<span class="numberofunlikes">('.count($unlikes).')</span>'; echo '<div class="downvoter-img">'; foreach( $unlikes as $unlike ) echo("<img src=\"".get_avatar_url(get_avatar( $unlike->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$unlike->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; $likescore = count( $likes ); $unlikescore = count( $unlikes ); return compact('likescore', 'unlikescore'); }and this code in single.php:
extract( $counts ); //restores compacted variables echo "Number of Likes: $likescore\n"; echo "Number of Unlikes: $unlikescore\n";But then I get no scores. Also, under the avatars now is shown the word “array”. Not sure why it isn’t working for me.
I also use this line of code to count the likes and unlikes:
$totalvotes = count($likes) + count($unlikes);
Is there a way I can use this again with the new code?I’ll try it again, but for now it isn’t working.
Forum: Hacks
In reply to: Remove text field from comment formThanks a lot again! I’m away for the weekend, so I will test it monday. Can’t wait to try it.
Forum: Hacks
In reply to: Remove text field from comment formThanks! The message function is working great.
About the other function. This code displays the avatars correctly:
function aigoo_get_score() { $comments = get_comments( array('post_id' => get_the_ID(),) ); $likes = array(); $unlikes = array(); foreach( $comments as $comment ) { if('unlike' == $comment->comment_content ) $unlikes[] = $comment; else $likes[] = $comment; } echo '<div class="upvoters" id="votes">'; echo '<span class="numberoflikes">('.count($likes).')</span>'; echo '<div class="upvoter-img">'; foreach( $likes as $like ) echo("<img src=\"".get_avatar_url(get_avatar( $like->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$like->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; echo '<div class="downvoters">'; echo '<span class="numberofunlikes">('.count($unlikes).')</span>'; echo '<div class="downvoter-img">'; foreach( $unlikes as $unlike ) echo("<img src=\"".get_avatar_url(get_avatar( $unlike->comment_author_email, 40 ))."\" class=\"tooltip\" title=\"".$unlike->comment_author."\" width=\"40\" height=\"40\">"); echo '</div>'; echo '</div>'; return $score; }If I add the following code in the above code…
$likescore=array(count($likes)); $unlikescore=array(count($unlikes));… and call it with:
echo $likescore;… then there is no like score displayed.
So the avatars are working great. Only now the working score code isn’t working anymore. I would like to use
echo $likescore;andecho $unlikescore;to hold the like and unlike score, because then I can do (I think) the following to calculate the real score:$score = $likescore - $unlikescore; $totalvotes = $likescore + $unlikescore;After that I can style the output with CSS.
So, the avatars are showing, but the score isn’t displaying anymore.
Forum: Hacks
In reply to: Remove text field from comment formNow I see there’s only another little problem: if another user with the same name but another email address give a like/unlike it says:
Duplicate comment detected; it looks as you already said that!
Is it possible to change this to something like:
Duplicate comment or not an unique username detected! If so, choose another username.
Forum: Hacks
In reply to: get_avatar shows the same avatar for all usersSolved!
Forum: Hacks
In reply to: Remove text field from comment formI found out the problem with the avatars. All I had to do was to change
$comment->comment_author_emailto$like->comment_author_emailand$unlike->comment_author_email.🙂
To return a value that can be assigned to a local variable for use elsewhere on the page looks like the best solution. I have to figure this out, because I’m a noob with this. Thanks for giving me a clue.
Forum: Hacks
In reply to: get_avatar shows the same avatar for all usersI found out. All I had to do was to change
$comment->comment_author_emailto$like->comment_author_emailand$unlike->comment_author_email.