Forums

[resolved] Custom Field - standard text instead when no content (5 posts)

  1. apa@imagix.dk
    Member
    Posted 3 years ago #

    I use this line to display a custom field (photograoher) on single.php...

    <?php $key="photographer"; echo get_post_meta($post->ID, $key, true); ?>

    Say the custom field "photographer" has not been added to a specific post. Is there anyway to just display a standard text then - "Not known" or something like that?

    Regards,
    Anders

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Something like:

    <?php
    $key="photographer";
    $custom=get_post_meta($post->ID, $key, true);
    if ($custom) {
    echo $custom
    } else {
    echo 'Not known';
    }
    ?>
  3. apa@imagix.dk
    Member
    Posted 3 years ago #

    That returns an error... Parse error: syntax error, unexpected '}', expecting ',' or ';' in /home...

  4. MichaelH
    Volunteer
    Posted 3 years ago #

    Oops, forgot a ;

    <?php
    $key="photographer";
    $custom=get_post_meta($post->ID, $key, true);
    if ($custom) {
    echo $custom;
    } else {
    echo 'Not known';
    }
    ?>
  5. apa@imagix.dk
    Member
    Posted 3 years ago #

    Thanks a million!

Topic Closed

This topic has been closed to new replies.

About this Topic