How do I show a hyperlink in a PHP if statement in WordPress?

I’m trying to add the following rollover image with a link inside of a PHP if statement.

but the hyperlink disappears when it is executed

Read More
<?php
      if(file-info) {
        echo " <a href="<?php the_field('file-info'); ?>">Access Application</a>";
      } else {
        echo "You are logged in!";
      }
?>

and

<?php if(file-info): ?>
<span class="rcg-line"><a href="<?php the_field('file-info'); ?>">
                       Access Application</a></span>
<?php else: ?>
<?php endif; ?>

I tried using both examples above and it doesn’t seem to work.

update

  • I’m using the advanced custom fields plugin for wordpress.

  • file-info is the varible its looking for in which the user entered and the_field is whats used to call it i guess

Related posts

2 comments

  1. So OP is talking about: http://www.advancedcustomfields.com/

    Try this…

    <?php if(get_field('file-info')): ?>
        <span class="rcg-line">
          <a href="<?php the_field('file-info'); ?>">Access Application</a>
        </span>
    <?php else: ?>
        <!-- Else case HTML would go here -->
    <?php endif; ?>
    

    Looks like the_field must call echo.

    Seems like there are docs on the vendors page. You need to unserstand distinction of ‘string’ literal and a variable. Also var_dump is your friend. Finally, please look at other posts which discuss how to enable PHP error reporting. Will save us all a lot of time.

  2. If the the_field function returns the desired value as a string, you need to echo it, and don’t forget that HTML will want quotes around the value:

     echo " <a href='"<?php echo the_field('file-info'); ?>"'>Access Application</a>";
    

    In your second example you will also want to put quotes around the value, and if the_field doesn’t echo the value to the client, you’ll need to echo it’s return:

    <a href='"<?php echo the_field('file-info'); ?>"'>
                       Access Application</a>
    

    Edit: If the the_field function is properly emitting the value, omit the echo in each example.

Comments are closed.