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.

My code validates and returns the custom field value. However, I can't get the value to return inside of the anchors.

Here is the code:

<div id="meta_mblink">
<?
if(function_exists('get_custom_field_data')) {
echo '<a href="'.get_custom_field_data('mblink', true).'"></a>';
} 
?>
</div>

This is what is returned:

<div id="meta_mblink">
http://yada.yadayada.yada:5000/pop.jsp?id=1711436
<a href=""></a>
</div>
share|improve this question

4 Answers 4

up vote 1 down vote accepted

This is how I would code this:

$url = get_post_meta( get_the_ID(), 'mblink', true );
if ( ! empty( $url ) ) {
    print '<a href="' . esc_url( $url ) . '">MBLINK</a>';
}
share|improve this answer
    
well that worked perfectly. thanks. –  glu Jun 21 '11 at 21:52

I suspect the problem is the custom function get_custom_field_data(). Can you post the function definition?

Most likely, the second argument corresponds to whether or not to echo the result. Try changing it to false?

EDIT

Here's an example of this function being defined in a tutorial:

<?php function get_custom_field_data($key, $echo = false) {
    global $post;
    $value = get_post_meta($post->ID, $key, true);
    if($echo == false) {
        return $value;
    } else { 
        echo $value;
    }
}
?>

I would recommend just using get_post_meta(), rather than using a wrapper function.

share|improve this answer
    
Ah hah! That was the situation I had that I was trying to recall in my above (or below) answer. I had an echo in an echo. I was struggling to remember why I had that problem. wordpress.org/support/topic/… Was the old forum thread where I got help for it. –  Rev. Voodoo Jun 21 '11 at 19:38
    
bennett yes that's the function I was using. So I've modified the function: echo '<a href="'.get_post_meta($post->ID, 'mblink', true).'"></a>'; but now nothing returns. I also tried to add a 'global $post;` before the echo but no luck. I dont understand why that wrapper function works but the get_post_meta() by itself does not. –  glu Jun 21 '11 at 20:59
    
@glu great! So does it work? –  Chip Bennett Jun 21 '11 at 21:00
    
yes. it shows in the source but not on the page. i'm assuming that's because there is no text for the link. however, when i add text to the argument like this: echo '<a href="'.get_post_meta($post->ID, 'mblink', true).'">MBLINK</a>'; it returns this <div id="meta_mblink"> <a href="">MBLINK</a> </div> –  glu Jun 21 '11 at 21:19

You need to set the second parameter to FALSE, (or just leave it blank) in the get_custom_field_data function. Do this:

<div id="meta_mblink">
 <?php
 if(function_exists('get_custom_field_data')) {
 ?>
 <a href="<?php echo get_custom_field_data('mblink'); ?>"></a>
 <?php
 }
  ?> 
</div>
share|improve this answer
    
this also worked. thank you. –  glu Jun 21 '11 at 22:09
<div id="meta_mblink">
 <?php
 if(function_exists('get_custom_field_data')) {
 ?>
 <a href="<?php echo get_custom_field_data('mblink', true); ?>"></a>
 <?php
 }
  ?> 
</div>

Maybe something like that? I recall having issues with echos busting out of html before and doing something like this worked for me

share|improve this answer

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.