3

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

<?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

rnevius
  • 26,578
  • 10
  • 58
  • 86
Eli
  • 61
  • 8
  • 2
    What is `file-info`. A constant? I'd start by turning on error reporting so you can be informed re. all the syntax /parse errors you are probably generating. – ficuscr Sep 18 '15 at 19:20
  • 1
    what does the `the_field` function do/return? – Joe Sep 18 '15 at 19:22
  • I think it should be `$the_field` , or otherwise , the real field name that you are calling ... same for `file-info` there is no function or constant on that name either in wordpress, nor in the custom fields plugin. maybe `get_field('file-info')` – Obmerk Kronen Sep 18 '15 at 19:34
  • 2
    seems i was wrong , `the_field` is somehow a function in ACF . but still it is called wrongly. I think you lack some basic `PHP` knowladge. see @ficuscr answer and read some docs ( also PHP ) – Obmerk Kronen Sep 18 '15 at 19:44

2 Answers2

1

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.

Joe
  • 25,000
  • 3
  • 22
  • 44
  • Hmm. I will try that give me a few to test – Eli Sep 18 '15 at 19:34
  • You should **never** `echo` `the_field()`, as that function already echoes a value. – rnevius Sep 18 '15 at 19:36
  • sure, hence the conditional in the first line: 'If the `the_field` function returns the desired value as a string'. If the `the_field` function actually sends the data to the output stream, don't echo its return. – Joe Sep 18 '15 at 19:42
  • 1
    from [docs](http://www.advancedcustomfields.com/resources/the_field/) `the_field` Displays the value of the specified field. (this is the same as `“echo get_field($field_name)”)` – Obmerk Kronen Sep 18 '15 at 19:50
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.

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • No. Going off the code I see on vendor page I see: `

    `... honestly who up-voted this question 3 times :) ? - ohh, @mevius I mean to say "The code must be calling echo because we do not need to" - sorry for the confusion.
    – ficuscr Sep 18 '15 at 19:53
  • it was I who was reading the sentence incorrectly. Whoops. Carry on. – rnevius Sep 18 '15 at 20:01
  • awesome, this worked perfectly. thanks a million and everyone else who commented. – Eli Sep 18 '15 at 20:14