functions - ACF not showing

admin2025-06-03  3

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:


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

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:

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

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:


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

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:

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

Share Improve this question edited Feb 1, 2019 at 8:09 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 1, 2019 at 8:03 VuckoVucko 1631 gold badge1 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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>';

Try

$hero = get_field('hero_title');

Instead of

$hero = the_field('hero_title');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748954855a315127.html

最新回复(0)