0

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!

1 Answer 1

1

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

2
  • Thanks (and sorry for the slow reply!) When I use this code, the whole sidebar becomes empty... Any idea? Thanks Commented Mar 19, 2013 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
    Commented Apr 16, 2013 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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