Forums

How do I get a Custom Field value without echoing key? (4 posts)

  1. nipponese
    Member
    Posted 2 years ago #

    Hi,

    I am trying to include a custom field value without displaying the key. Previously I would have used a shortcode plugin to do this, but echoing a do_shortcore seems a little hacky to me. Is there a better way?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

  3. nipponese
    Member
    Posted 2 years ago #

    I'm not sure I really understand this function.

    Lets say I only have one key called descriptions, I think the php should look like this:
    <?php $key_description_value = get_post_meta($post->ID, 'Description', true); echo $key_description_value;?>
    or
    <?php $key_description_value = get_post_meta($post->ID=$post_id, 'Description', true); echo $key_description_value;?>

    but I get nothing...

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    <?php $key_description_value = get_post_meta($post->ID, 'Description', true); echo $key_description_value;?>

    Will work, providing it's in the loop (so it has a post ID) and the custom field exists for that post.

    You could narrow down the problem by IF/ELSE'ing the field value check. You could also check the post ID is valid.

    <?php if(!$post->ID) echo 'post ID doesn\'t exist'; ?>
    <?php $key_description_value = get_post_meta($post->ID, 'Description', true); ?>
    <?php if(!$key_description_value) { echo 'nothing found'; } else { echo $key_description_value; }?>

Topic Closed

This topic has been closed to new replies.

About this Topic