Support » Fixing WordPress » Conditionals Around Custom Field Value Calls Not Working

  • Resolved justbishop


    Bear with me here. I’m using the Prospress plugin, which creates the custom post type “auctions”, and have hacked it to support custom fields. I am then using the Custom Field Template plugin to add 4 entries for users to enter URLs for remotely hosted images (Photobucket, etc.) I do not want to host their image files.

    Anyway, I’m then calling the values of these 4 fields (which are image URLs) into the Prospress single auction template, as shown here:

    The following test auction post has the first 3 field values filled in with image URLs, and the 4th is left blank:

    So why would all 4 spots be displaying the “else” content of “LALALAAA”? Are my conditional statements somehow wrong? Should they be coded differently because they are calling custom field values from a custom post type?

    Just for fun, I removed the conditional stuff from around each custom field call, and they work like a charm (lightboxed full sized popups, even), but without the conditional, spot 4 renders as a broken image, because the custom field it’s calling was left blank. I can’t guarantee that users will always have 4 images to enter for their post, so I really would like to conditionalize them!

    P.S. I know the end result looks horrid at the moment. I’m just trying to get everything functioning correctly before tackling the cosmetics.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You are using two functions – get_post_meta() and get_post_custom_values().
    One is enough. For example. Notice that $single parameter here is set to true.



    Forum Moderator

    Try using <?php if ( $image1 != '' ) : ?>

    OK, I think I implimented both suggestions correctly, but it didn’t seem to change anything on the “Hello Bidders” post as linked to above. This is the code for the custom field calls that is currently in use on the template file (after making the two suggested changes):

    (And sorry it’s a bit hard to read, IDK why pastebin spaces my code all weird :/)



    Forum Moderator

    Have you tried simply dumping the output of get_post_meta() to the screen so you can see exactly what it is returning?

    No, but it does return the values of the custom fields perfectly if I remove the conditional statements altogether. I figured that was a pretty accurate indicator that the issue is with the conditional code *shrug*

    wrong html
    <img src="<?php echo get_post_meta($post->ID, 'thumbnail', true) ?>" width="140px"</img>
    change to
    <img src="<?php echo get_post_meta($post->ID, 'thumbnail', true) ?>" alt="<?php the_title(); ?>" width="140" />
    As a bonus – another way:

    $image1 = get_post_custom_values('thumbnail');
    if (isset($image1[0])) {
        echo '<img src="'.$image1[0].'" alt="'.get_the_title().'" width="140" />';
    } else {
        echo 'No image';

    AHA! That last code snippet worked! Thanks so much 🙂

    I did re-link the image “thumbnails” to their full sized versions, but everything seems to be functioning just as I’d hoped.

    Thank you for the time, everyone!!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Conditionals Around Custom Field Value Calls Not Working’ is closed to new replies.