1

I am trying to make my webpage display data in a custom meta field only if there is data to display. This is what I have right now.

I tried the following code, but the echo statement fails: the label 'State' doesn't display but the field data of 'genesis_custom_field('state')' shows up.

<?php
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(genesis_custom_field('state'))
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>

1 Answers1

0

Try this way:

<?php
// works on php 5.6
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(!empty(genesis_get_custom_field('state')))
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>

<?php
// works on php < 5.6
add_action( 'genesis_before_entry_content', 'state_meta' );
function state_meta() {
    if(genesis_get_custom_field('state') != '')
    {
        echo '<br /><strong>State</strong>: ';
        genesis_custom_field('state');
    }
}
?>
  • Your answer works perfectly in my sandbox on Site5 (using PHP 5.6.22) but when I try the same thing on the live site (hosted very unfortunately at GoDaddy and using PHP 5.4.), the site crashes. The error I see is "Fatal error: Can't use function return value in write context in..." then it identifies the line that includes your code starting with if(!empty... I'm guessing this is a PHP problem. Do you have any suggestions? Thanks SO much!!!!! – user3044291 Jul 08 '16 at 03:52
  • @user3044291 Change that line to and try: ` – Muhammad Sumon Molla Selim Jul 08 '16 at 06:11
  • That worked PERFECTLY. Thank you, thank you, thank you for taking the time to help me with this! – user3044291 Jul 08 '16 at 19:54
  • @user3044291 welcome. it would be great if you accept the answer and upvote when you can. – Muhammad Sumon Molla Selim Jul 08 '16 at 20:30