WordPress.org

Ready to get started?Download WordPress

Forums

Custom Field Template
How to get the Key to Show? (3 posts)

  1. Chris
    Member
    Posted 3 years ago #

    Kinda a noob question but when it comes to this plugin and custom fields that is what I am.

    How do I get the Key to show with the value?

    Here is the Code I am using in my page template: <?php echo get_post_meta($post->ID, 'Key Goes Here', true); ?>

    I want the key to show up with the value but if there is no value then I don't want anything to show up. Is this possible? Thanks!

    http://wordpress.org/extend/plugins/custom-field-template/

  2. stvwlf
    Member
    Posted 3 years ago #

    You already know what the key is, in order to look up the value. Is the key hard coded in apostrophes or in a variable? If hard coded you can do it like this:

    <?php if ( $value = get_post_meta($post->ID, 'Key Goes Here', true) ) {
      echo 'Key Goes Here' . ' value = ' . $value;
    } ?>

    If key is in a variable

    <?php if ( $value = get_post_meta($post->ID, $key, true) ) {
      echo $key . ' value = ' . $value;
    } ?>

    Change the echo statement to format however you want the output.

  3. Chris
    Member
    Posted 3 years ago #

    Hey Steve,
    Thank you for the response! I see you are from Ashland, I am from Portland!

    The hard coded snippet is just what I was looking for. I wanted to make it work when there was a value to the key on the corresponding page and disappear when there is no value to the key. Worked perfectly! Hopefully this will help someone else out along the way as well.

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic