Forums

[resolved] Images specified in Custom Fields (10 posts)

  1. Syni
    Member
    Posted 6 years ago #

    Okay... here's one I hope I can explain correctly.

    I want to have an image with each post, either an image that says "no image" or an image that is specified as a custom field in a post (using that key, value thing right?)... okay, so now how would I code it to say to show the image if that value exists within a post, and to show the "no image" image if there is no image value specified in the post?

  2. Syni
    Member
    Posted 6 years ago #

    Well, reading the Codex article about Custom Fields, I don't think you can specify images as meta data... so is there any possible way to do this?

  3. Kafkaesqui
    Moderator
    Posted 6 years ago #

    "I don't think you can specify images as meta data."

    Why so quick to decide that?

    There a number of ways you can go about this. My suggestion is this bit of code:

    <?php if(get_post_custom_values('img')) :
    foreach(get_post_custom_values('img') as $value) {
    echo '<img src="' . $value . '" />';
    }
    else :
    echo '<img src="no-image.gif" />';
    endif; ?>

    Note this assumes use of 'img' for the custom field key. You may want to spiff up the img tags with height, width and title attributes. I also assume you're providing the full filename (and possibly path to image) as the field value, when you can use the script to take care of much of that. For example, if images reside in /images/, and all images will be GIFs, you could just use the name of the file without extension, and change the code to:

    echo '<img src="/images/' . $value . '.gif" />';

  4. Syni
    Member
    Posted 6 years ago #

    Haha, well, I guess I have a hard time thinking outside the box when it comes to PHP and stuff like that. Thanks Kafkaesqui, I was hoping that you'd come up with something. :) I'll try this out.

  5. Syni
    Member
    Posted 6 years ago #

    Awesome! It works!

  6. cbit
    Member
    Posted 6 years ago #

    I'm very interested in this too..

    are there any plugins in existance that allow file upload and insertion of the image url into the value field of a custom field? (or something similar for conveniently adding images to custom fields).

  7. 3stripe
    Member
    Posted 6 years ago #

    cbit, did you find anything to insert an image automatically into a custom field? i imagine you could do this by hacking an image plugin, but i'm not enough of a programmer to do this :-P

  8. 3stripe
    Member
    Posted 5 years ago #

    I'm still really interested in this. I don't have time to make my own thumnails for all posts (especially with other folk adding posts themselves as well)... it needs to be automated....

  9. hamzaz
    Member
    Posted 5 years ago #

    am using the whole Post Instad ..... thanx for that tip

  10. hamzaz
    Member
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic