WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
): ?>
            <P>Show Link</P>

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

<?php endif; ?>


share|improve this question

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

$post_meta = get_post_meta(
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.


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? – tfrommen Apr 16 '13 at 12:33

Your Answer


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.