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 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>'); ?>

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
<?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>

share|improve this answer
    
thanks but the problem with that is that the link will appear even when I don't add content to the custom field. –  AHCo Dec 19 '11 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 Dec 19 '11 at 17:05
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.