0

This is the code I've added to a single.php file and I need to NOT display the Article Reference echo line, if the custom field value is empty.

I am using Genesis, if that matters.

How should this be written? Also, is there a better way to write this code?

Thanks, Louise

function custom_do_grid_loop() {
// Intro Text (from page content)
echo '<div class="page hentry entry">';
echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
if ( has_post_thumbnail() ) {
    echo '<div class="import-image">' . get_the_post_thumbnail() . '</div>';
} 
echo '<div class="entry-content"><p>' . get_the_content() . '</p>';
echo '<div class="field_name"><strong>Article Reference:</strong> ' . genesis_get_custom_field( 'article_reference' ) .'</div>';

echo '</div><!-- end .entry-content -->';
echo '</div><!-- end .page .hentry .entry -->';
}
2
  • Thanks! I'm learning so much from these examples. What if I want to check to see if the $meta value is a specific number? For example, if the value is 20.
    – Louise
    Commented Oct 31, 2013 at 19:10
  • If any answer helped you or fixed your problem you should consider it as best answer. It would also be better if you commented on my answer so i get notified. See my edited answer well. Commented Oct 31, 2013 at 20:45

1 Answer 1

2

Add a little check to your code

    function custom_do_grid_loop() {
    // Intro Text (from page content)
    echo '<div class="page hentry entry">';
    echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
    if ( has_post_thumbnail() ) {
        echo '<div class="import-image">' . get_the_post_thumbnail() . '</div>';
    } 
    echo '<div class="entry-content"><p>' . get_the_content() . '</p>';
    //Add this condition to your code
    $meta = genesis_get_custom_field( 'article_reference' );
    //Remove white space from both sides,
    if( !empty(trim($meta)) ){
        echo '<div class="field_name"><strong>Article Reference:</strong> ' . genesis_get_custom_field( 'article_reference' ) .'</div>';
    }
    echo '</div><!-- end .entry-content -->';
    echo '</div><!-- end .page .hentry .entry -->';
    }

If its not empty show it, otherwise don't.

Edited:

You can do something like this for further conditions

    if($meta == 20)
    {
        echo 'do 20';
    }
    elseif ($meta == 21)
    {
        echo 'do 21';
    }
    else
    {
        echo 'If all condition fails this will be echo';
    }
4
  • I've seen people use this before as well: if($meta != '')
    – Stephen S.
    Commented Oct 31, 2013 at 20:58
  • There are many ways for checking the empty values isset($var) $var == "" empty($var) if($var). But empty and $var == "" don't check for spaces from either side. See my answer Commented Oct 31, 2013 at 21:30
  • Thanks, I was able to get my code to work with your examples. I'd like to vote but I've not been a member long enough. ;)
    – Louise
    Commented Nov 1, 2013 at 1:56
  • How do I make accepted?
    – Louise
    Commented Nov 1, 2013 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.