Results 1 to 2 of 2

Thread: Simple checking php script for Author Bio in WordPress...

  1. #1
    Join Date
    Aug 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Simple checking php script for Author Bio in WordPress...

    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 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!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espaņol | Deutsch | italiano | portuguęs | catalā | un peu de franįais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •