Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use 'if' 'else' to display a link button if I have a value in the 'TourURL' custom field, and if not, nothing will display but can't seem to get the if code to work:

<?php if(global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'TourURL', true);
    wp_reset_query();
): ?>
            <P>Show Link</P>

<?php else : ?>
            <P>No Link</P>

<?php endif; ?>

Thanks!

share|improve this question
add comment

1 Answer

I think, you're mistaken in how if works...?

$post_meta = get_post_meta(
    $GLOBALS['wp_query']->post->ID,
    'TourURL',
    true
);
if (! empty($post_meta)) echo '<p>Show Link</p>';
else echo '<p>No Link</p>';

This should do what you want - if I understood you correctly.

// UPDATED

share|improve this answer
    
Thanks (and sorry for the slow reply!) When I use this code, the whole sidebar becomes empty... Any idea? Thanks –  emailsforben Mar 19 '13 at 14:30
    
Well, then sorry for my really slow reply. I must've overlooked your comment. Anyway, I updated the above code (and tested it). Now, it is working as expected, at least for me. What about you? –  t f Apr 16 '13 at 12:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.