0

I have a (weird) issue - can't get the ACF field to work on my page. I have a simple text_field and it shows on the Homepage page section:

ACF settings: enter image description here


Homepage settings (when I refresh the panel it shows Lorem ipsum so it's saved in the databse): enter image description here

And in my index.php page I have:

<?php
  $hero = the_field('hero_title');
  /* tried also  */
  /* $hero = get_field('text_field'); */

  echo '<h1>'.$hero.'</h1>';
?>

Where it shows the blank <h1></h1> on my page:
enter image description here

What am I doing wrong? I also tried with group type and I have the same issue.

2

the_field() function shows the value and it doesn't get/return anything. Like the_title() and other template tags starting with the_.

It means that:

This line prints the value and $hero variable is empty.

$hero = the_field('hero_title');

This line prints only <h1> tags.

echo '<h1>'.$hero.'</h1>';

What you want is:

echo '<h1>' . get_field('hero_title') . '</h1>';
| improve this answer | |
  • I tried but it also returns the blank value - <?php echo '<h1>Title: ' . get_field('hero_title') . '</h1>'; ?> where the string Title is shown. – Vucko Feb 1 '19 at 8:10
  • So most probably you set it to different post and get it for different post. echo get_field('hero_title'); should work like a charm. – Krzysiek Dróżdż Feb 1 '19 at 8:11
  • But I'm not using a post - it's a single page theme. I have only one page - Homepage and the ACF is set to it. – Vucko Feb 1 '19 at 8:15
  • Update - well this is weird -> if I use ACF rules Page type to Front page it works, but if I use Page to Homepage it doesn't? – Vucko Feb 1 '19 at 8:16
  • 2
    It is not weird at all. ACF fields (like all custom fields) are assigned to some post object (pages are posts too). If you use static page as front page, then that page is stored as global $post and the_field will get custom fields from that post. If you use Homepage, then global $post will contain some post, so you'll be getting custom fields from that post... – Krzysiek Dróżdż Feb 1 '19 at 8:18
1

Try

$hero = get_field('hero_title');

Instead of

$hero = the_field('hero_title');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.