treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Calling a string in a link

  • Here's my code. In case it isn't obvious what I'm trying to do, I want to only show the image if there is info in the field. And the link around the image uses the info in the field. The mailto: 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 } ?>
    
  • Try this:

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

    The solution is generally !empty (not empty). But for details, take a few minutes and read up on this: http://php.net/manual/en/function.empty.php isset might work better for you.

    EDIT: Also, your $empty variable right after the mailto is missing a semicolon. ;)

  • Also, <?php $email ?> doesn't work (unless you have some reallyold/reallyfunky configuration). It needs to be either:

    <?php print $email; ?>

    or

    <?= $email ?> (works ONLY IF short_open_tag is enabled, or with PHP 5.4.)

  • Thanks for looking at this.

    <?php print $email; ?> knocks it out of the HTML it's in. So instead of an image wrapped in a URL, I get person@emailaddress.com"> IMAGE

    pmac627, your solution doesn't work for me either. I still get a blank value for my string. And I've tested other fields, to make sure it isn't just this field. I will look into isset though. Thanks for the recommendation.

  • Perhaps try echo $email; instead? Good call, @traq. I can't believe I missed that as well.

    Try to echo $email somewhere on the page to make sure that it actually contains a value.

  • This will do the trick:

    Don't know why the markup of this is so shit. But copy it in your code and make it look more nice.

    <?php // Get the e-mail variable $email = types_render_field ("email"); // Print out the e-mail variable, to make sure you've got one echo "<pre>" . print_r($email) . ""; // Check for an e-mail address if ( !empty($email) ) { ?> <a href="mailto:<?php echo $email; ?>"> <img src="<?php bloginfo( 'template_url' ); ?>/images/icon-email.gif" width="24" height="24" /> </a> <?php } ?>

  • Nope, I've tried echo before. It does the same thing as print -- knocks it out of the href link and displays it on the page.

    And Vermaas, unless I'm missing something, yours is the same as pmac627 with just the print_r ($email) to test the content of the string. Am I getting that wrong?

    I'm hoping to have this site online for testing in a few days. Perhaps I can post a link then and get your thoughts.

    Again, thanks for trying.

  • 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.

    Thank you all for your help. (Especially since I was missing that echo in addition to everything else.)