• thealchemyst

    (@thealchemyst)


    While developing my first WP theme I wanted to have an image for every post. On my home page I’m listing the 10 latest posts consisting of category name, title, summary and image.

    So, my main question is what’s the best method to handle an image along with a post? I don’t want to upload and put into my content section, I want to be separate, so I thought of using a custom field, but then I can’t find a way to parse the value in HTML. Is there any alternative?

Viewing 3 replies - 1 through 3 (of 3 total)
  • alexleonard

    (@alexleonard)

    Radu Capan has a nice little plugin for automatically finding the first image from your post and displaying it in your excerpt.

    https://www.ads-software.com/extend/plugins/thumbnail-for-excerpts/

    This might do what you’re looking for. Otherwise I think there is a plugin that adds the media uploader to the excerpt box.

    Finally, I use the custom meta option quite a lot, as it gives me complete control.

    The way I usually do this is to put a URL pointing to my preferred image for the post, and then in the home page loop I’d add a line of code like this:

    <?php
    $postthumb = get_post_meta($post->ID, "postthumb", true);
    if (!empty($postthumb)) {echo "<img class='postthumb' alt='Image for " . $post->post_title ."' src='" . $postthumb. "' />";}?>

    In plain english this says

    1. Assign the value of the meta key “postthumb” to a variable called $postthumb
    2. if $postthumb is not empty, then display an image.

    I hope that makes sense!

    dhurst

    (@dhurst)

    I’d recommend Yet Another Photoblog — the previous post is good, but you have to upload an image in your content.

    Dgold

    (@dgold)

    You were correct in your first instinct. This can be done with Custom Fields, out of the box wordpress.

    Let’s say custom field Key= “My Special Image For This Post 300×250 Pixels”. In the Value you can put either:
    1) The full path URL to the image https://yoursite.com/images/bluebird-post-300x300.jpg
    2) Just the image’s filename like bluebird-post-300x300.jpg
    3) or put the whole image HTML code <img src="https://yoursite.com/images/bluebird-post-300x300.jpg" alt="My awesome Bluebird picture">
    Depending on which method you choose (for what is in the Value), will determine what codes you need to use to display it in your theme. But it’s pretty much what you image. Hard-code part of it, and pull in the Custom Field where that variable needs to fill in.

    Search Google for post images custom fields. There’s many versions of the instructions, like,
    https://www.junglejar.com/2008/10/11/wordpress-customfields/

    https://wordpressgarage.com/plugins/images-thumbnails-and-custom-fields-in-wordpress/

    https://scribu.net/projects/custom-field-images.html

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘What’s the best way to have a photo along with a post?’ is closed to new replies.