• I’m using

    <?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'FIELD', true); ?>

    to get custom fields in my sidebar. I put them all in a list like this:

    <li><?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'FIELD1', true); ?></li>
    <li><?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'FIELD2', true); ?></li>
    <li><?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'FIELD3', true); ?></li>

    I’m wondering how I could set up an if statement to see if I have actually entered the field, and only show it if it isn’t empty.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try using get_post_custom($postid) it is much more efficient. It returns ALL the custom fields defined for a post/page id. You don’t have to remember the details of how many custom fields you entered, or even if you entered any at all! If any exist then they get echoed to the screen, else nothing is outputted.

    I have rewritten your code sample to be:

    <?php
        global $wp_query;
        $postid = $wp_query->post->ID;
    
        $custom_fields = get_post_custom($postid);
        foreach($custom_fields as $custom_field) { // loop incase of > 1 custom field
            echo "<li>$custom_field</li>";
        }
    ?>

    Hope it works for you.. 🙂

    Thread Starter rinse

    (@rinse)

    Thank-you, but doesn’t quite solve my problem. Another issue I should have mentioned is that my side bar is split up in several sections. One section the custom fields just pull a line of text, the next they pull links, and the other one they pull images (which i then use another field to get the image to link somewhere)

    So I really need the if statement on only a couple of the fields, mainly the image one with a link. I have it like so:

    <a href="<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'IMAGELINK', true); ?>" target="_blank"><img src="<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'IMAGE', true); ?>" /></a>

    So I think i just need something that says if custom field “IMAGE” is equal to something show the above, if not show nothing.

    OK well I had no idea of this extra information as you did not specify that in your original post! 😉

    What I have provided should give you enough to complete what you need. Just test the returned custom field array using the in_array function:

    http://php.net/manual/en/function.in-array.php

    You can use this to see if there is an image in there somewhere. If there is then great, you can go ahead and build your link to the image, or anything else you need.

    <?php global $wp_query; $postid = $wp_query->post->ID; ?>
    <?php $field1 = get_post_meta($postid, 'FIELD1', true); if($field1) echo '<li> . $field1 . '</li>; ?>
    <?php $field2 = get_post_meta($postid, 'FIELD2', true); if($field2) echo '<li> . $field2 . '</li>; ?>
    <?php $field3 = get_post_meta($postid, 'FIELD3', true); if($field3) echo '<li> . $field3 . '</li>; ?>

    and:

    <?php global $wp_query; $postid = $wp_query->post->ID;
    $imagelink =  get_post_meta($postid, 'IMAGELINK', true);
    $image = get_post_meta($postid, 'IMAGE', true);
    if($imagelink && $image) {
    echo '<a href="' . $imagelink . '" target="_blank"><img src="' . $image . '" /></a>'; } ?>
    Thread Starter rinse

    (@rinse)

    Thank-you both,

    Alchymyth the one for the images works perfectly but the top one is giving me

    Parse error: syntax error, unexpected ‘/’ in /home/content/53/6524553/……

    my bad
    yes, it would do that – with a missing ‘ in each line after the '<li> ;

    corrected:

    <?php global $wp_query; $postid = $wp_query->post->ID; ?>
    <?php $field1 = get_post_meta($postid, 'FIELD1', true); if($field1) echo '<li>' . $field1 . '</li>; ?>
    <?php $field2 = get_post_meta($postid, 'FIELD2', true); if($field2) echo '<li>' . $field2 . '</li>; ?>
    <?php $field3 = get_post_meta($postid, 'FIELD3', true); if($field3) echo '<li>' . $field3 . '</li>; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Don't show custom field if left blank’ is closed to new replies.