WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditionals Around Custom Field Value Calls Not Working (8 posts)

  1. justbishop
    Member
    Posted 2 years ago #

    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: http://pastebin.com/Tv8bFm4V

    The following test auction post has the first 3 field values filled in with image URLs, and the 4th is left blank: http://www.riotcart.com/marketplace/?auctions=hello-bidders

    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.

  2. vjpo
    Member
    Posted 2 years ago #

    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.

  3. esmi
    Forum Moderator
    Posted 2 years ago #

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

  4. justbishop
    Member
    Posted 2 years ago #

    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):

    http://pastebin.com/g1V5nQYs

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

  5. esmi
    Forum Moderator
    Posted 2 years ago #

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

  6. justbishop
    Member
    Posted 2 years ago #

    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*

  7. vjpo
    Member
    Posted 2 years ago #

    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';
    }
  8. justbishop
    Member
    Posted 2 years ago #

    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!!

Topic Closed

This topic has been closed to new replies.

About this Topic