• I’m moving to WordPress from a custom CMS. We used to give articles/posts an image that was then displayed on our home page.

    Well, this isn’t supported by WP by default, but that’s cool because I plan on using a custom field to do this. I’ll name the custom field ‘image’ if there is an image available for a particular post.

    So, I create a custom field ‘image’ with the value ‘test.jpg’

    In the loop, I’ve got:

    <?php if (get_post_meta ($post->ID, "image", $single = true)) { ?><img src="<?php get_post_meta($post->ID, $key, $single = true); ?>" /><?php } ?>

    It doesn’t seem to work 🙁 and I’m not to sure where to go from here.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Did you take a look to this template tag?
    http://codex.wordpress.org/Template_Tags/the_meta
    It displays whatever you put in the value field (if empty, nothing is displayed). BTW, ‘test.jpg’ probably wouldn’t be enough… sometimes even a longer relative path with directory doesn’t work.

    the_meta() will work, but adds custom field content in an unordered list (and displays both the key and value). Here’s another option:

    <?php if(get_post_custom_values('image')) : foreach(get_post_custom_values('image') as $image) : ?>
    <img src="<?php echo $image; ?>" />
    <?php endforeach; endif; ?>

    Follow moshu’s recommendation on the image path (though you could add this to the code above).

    Thread Starter commanderbond

    (@commanderbond)

    I found a plugin, but if I can use wp code, then I’d rather do that – so thanks Kafkaesqui, I’ll use your above code snippet.

    Nice snippet Kafk 🙂

    I spent ages searching for this, seems a lot of people have problems with custom field values…

    Kafkaesqui, you bum! I spent two hours to find a trick to display this kind of meta.

    I wonder why a template tag to images wasn’t implemented in the WP core.

    If I understand this, is this what my code would look like to pull one of my three custom fields data (Assigned Month is the name of custom field):

    <?php if(get_post_custom_values(‘Assigned Month’)) : foreach(get_post_custom_values(‘Assigned Month’)): ?>

    I’m not want to display an image, so I took that off. If I use <?php the_meta(); ?> I get a list of the custom fields and the data. But anything else I’ve tried to just display one of the fields makes my page just stop (I only see the header).

    What I really want to day (in the long run) is use the Assigned Month data in create a link to this site:
    http://www.oneyearbibleonline.com/january.asp … I would be replacing “january” with the data from Assigned month.

    Thoughts?

    This shows my lack of coding knowledge! I had spaces in my code before the <?php …; ?> … apparently that’s not a good thing! I’m now able to pull just the custom field I want (I also got the Get-Custom plugin).

    Now to work on using it to grab the link above. I’m assuming that will be relatively simple 🙂

    I found a better way to do what I’m trying to do here … just in case others are looking for this somehow …

    .asp”>TODAY’S READING – choose your own version!

    With this code I was able to link to the appropriate web site (at least for the month) based on the post date. I don’t know why I didn’t think of this sooner! I didn’t need custom fields after all.

    Awesome code Kafk! I’ve been looking for how to do that for over a week… GREAT STUFF!

    I may have just not been looking in the right place, but this seems to be a woefully under documented feature, (custom fields).

    -Mr. Crimson

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘If a custom field exists for a post, output the value’ is closed to new replies.