Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Adding a URL from custom field in php & WP theme WordPress

Hi all

I have been struggling with this for hours and hours now, hopefully someone here can help.

I have a post, which has a custom field as a website URL.

I want to change single.php to state:

Website: where that wording is also the href which is called from the custom field.

The code looks like this:

if (get_post_meta($post->ID,'weblink1',true)) :
echo '<h3><div class="exhibitor-info">';
echo "Website: " . get_post_meta($post->ID,'weblink1',true);
echo '</div></h3>';

This displays everything, but I simply cant get it to include the href no matter what I try, to make the href the weblink1 custom field.

Can anyone help me out? Much appreciated.

Answers (1)


Pixel Coder answers:

If called outside the loop make sure to set

global $post;

If within the loop, that should be fine.

Without seeing the whole file it's kinda hard to say. That's how you call custom fields though.

Rob Llewellyn comments:

Hi Alistair, thanks for the speedy reply, much appreciated.

Yea that calls the custom field no problem, but if I want to make that a hyperlink as well, I need to call the custom link again to be the URL.

<a href=" http:// . get_post_meta($post->ID,'weblink1',true);"> HERE </a>

I dont know what that piece of code is meant to look like as an href in my code. It never works, I must be missing some syntax for the php in this instance.....?

Pixel Coder comments:

Hi Rob,

No problems, email notifications are handy.

Here you go.

<?php $website1 = get_post_meta($post->ID,'weblink1',true); ?>
<?php if(isset($website1)) : ?>
<a href="<?php echo $website1; ?>"><?php echo $website1; ?></a>
<?php endif; ?>

That should do it and thanks for clarification.


Rob Llewellyn comments:

Thanks Alistair. I wish I'd dropped a question in here an hour or so ago!!!

I've closed the question as resolved and its donated to the site.

I take it you get the kick back from that?

Thanks again, awesome.