0

I want to show the image only if there is info in the field. And the link around the image uses the info in the field. The mailto: field is coming up blank. So I'm guessing/hoping I'm doing something wrong with the way I'm printing/calling it. I would greatly appreciate any help.

<?php $email = types_render_field ("email");
          if ( $email ) { ?>
           <a href="mailto:<?php $email ?>"><img src="<?php bloginfo( 'template_url' ); ?>/images/icon-email.gif" width="24" height="24" /></a>
<?php } ?>

In case it's relevant, I'm using the Types plugin to create my custom fields.

2 Answers 2

1

Change,

<?php $email ?>

to...

<?php echo $email; ?>

as you need to echo the result of what's held within $email as the function types_render_field would be using return within the function (if you dig into its core) to return the data instead of directly printing it to screen.

<?php $email = types_render_field ("email");
          if ( $email ) { ?>
           <a href="mailto:<?php echo $email; ?>"><img src="<?php bloginfo( 'template_url' ); ?>/images/icon-email.gif" width="24" height="24" /></a>
<?php } ?>
2
  • Thanks very much. When I echo it, it pulls it out of the href link and displays [email protected]" (with the quote at the end, from the href code. Any idea how to prevent that from happening?
    – SBG
    Oct 17, 2012 at 1:38
  • Sorry, add a semi-colon ; after your variable declaration - so <?php echo $email; ?> (snippet updated)
    – Adam
    Oct 17, 2012 at 4:51
0

I figured it out! It had to do with the way the custom field had been created. When it was an "email field", it gave me that big mess. When I made it just a single line text field, it was fine.

3
  • Hmm probably shouldn't have made a difference, it was more likely the missing semi-colon that was causing the improper output as mentioned in my update below.
    – Adam
    Oct 17, 2012 at 4:53
  • the semi-colon and echo helped with the field return, thanks. But the new problem that presented itself -- the inability for the field's value to exist within an href tag -- was fixed by changing the field type.
    – SBG
    Oct 18, 2012 at 4:11
  • Peculiar to say the least. It shouldn't have an effect on printing the value within a href attribute, however, changing the field type still suffices. I wonder if adding true as in $email = types_render_field('email', true); would have any bearing? That aside, if the problem is now solved for you that's great, then you cane mark and close out this question.
    – Adam
    Oct 18, 2012 at 4:30

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.