Log in

View Full Version : Simple checking php script for Author Bio in WordPress...



bennyy007
06-29-2010, 09:19 AM
Hello,

I made this script to check to wherever the 'Author Bio' is empty or not if empty then display a little message saying 'This author does not have a description yet...' but if the author does have a description then it will display their bio.

But the problem I'm having is that it displays the 'Author Bio' also the message that is shown when the Author doesn't have a bio???

Here's the code:



<?php
if ( the_author_meta('description') == '' ) {
echo '<em>This author does not have a description yet...</em>';
} else if(the_author_meta('description')) { echo the_author_meta('description'); }
?>


I've tried changing it around using different methods but yet fail as it still does the same...So if anyone could kindly help me with solving this I would greatly appreciate it! -- Many thanks in advance! :)

djr33
06-30-2010, 08:22 AM
You're checking first if it is blank, then later if it exists. The first will return true when it's blank and false when there's a value. The second will ALWAYS return true because it always is set, at least set to a blank string. (Blank and not set are different-- one is a blank VALUE, and the other is no value at all.)

The simple answer here is that you should use if (condition) and then else, rather than "else if". You're looking for a two-possibility operation: it's set or it's not, so you can use else to catch all cases that don't match the if.

While == '' may work for you, there is a complication: you are checking using double equals (==), and not triple equals (===). They do the same thing except that triple also forces that the type of the values be the same. Using double equals is usually enough, but when you're being specific about what type of data is there, the double equals will simplify things. It will consider ALL of the following to be "equal":
FALSE; 0; '' (empty string); null

It's PROBABLY best to keep it like it is, but if you run into problems, switching == for === might help, or using another thing to check it. The function empty() is usually helpful in these cases, but I don't believe it works on functions (rather than just variables), so it may not work with the_author_meta() in this case.