I'm using the following to display a custom field value in my posts:

<?php echo get_post_meta($post->ID, 'wikipedia', true); ?>

How do I add this value to the output, where '$tag' takes the tag of the post:

- <a href="wikipedia.org/wiki/**$tag**">Wikipedia</a>

What I've tried, unsuccessfully:

<?php echo get_post_meta($post->ID, 'wikipedia', true . '- <a href="wikipedia.org/wiki/$tag">Wikipedia</a>'); ?>

1 Answer 1

<?php $tag = get_post_meta($post->ID, 'wikipedia', true); ?>

<?php if (isset($tag) && $tag != '') : ?>
    <a href="wikipedia.org/wiki/<?php echo $tag; ?>" title="Wiki this!">Wikipedia</a>
<?php endif; ?>

To explain it a bit: First you put the data in a variable, then if the variable has some data, it will show the link and echo the data after /wiki/. You can use the variable also on other places like so: <a href="wikipedia.org/wiki/<?php echo $tag; ?>">Wikipedia (<?php echo $tag; ?>)</a>

  • thanks but the problem with that is that the link will appear even when I don't add content to the custom field.
    – AHCo
    Commented Dec 19, 2011 at 4:51
  • You don't need the isset check, it'll come back as an empty value if it's not set or if it's empty, so it's better to use if( !empty( $whatever ) ) for the conditional block.
    – t31os
    Commented Dec 19, 2011 at 17:05

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.