Wordpress ACF get_field( ) not returning value

一曲冷凌霜 提交于 2020-01-02 03:27:46

问题


I am using the advanced custom field plugin for Wordpress. I am having difficulty displaying a field on my page.

Basically I've created a field group and assigned id's to the members of that group. I then use the get_field('field_name') function to store the value of this field in a variable and echo it on the screen. However this is returning false.

I've also tried using the_field('field_name') but this returns null. I then read somewhere If you are trying to access a field outside of the Wordpress loop you must pass the post id as a parameter to the get_field()/the_field() methods.

I've tried that and still the same result...Does anyone have any idea as to what is the problem?

This is my code:

<?php get_header();
      $postID = get_the_ID();
      the_field('the-title', $postID); //Nothing being returned...
      die(); 
?>

回答1:


If you're using WP_Query() before using get_field(), you need to reset the query using wp_reset_query() function. I hope it'll solve this issue.




回答2:


You're using get_the_ID() outside of the loop.

http://codex.wordpress.org/Function_Reference/get_the_ID

You could try:

global $post;
the_field( 'the-title', $post->ID );

But this would depend on what page you're on.

Which template file is this being used in?




回答3:


You need to create a loop, then inside that loop you can retrieve the data.

<?php while( have_posts() ) : the_post() ?>
     <?php $variable = the_field('the-title'); ?>
<?php endwhile; ?>


来源:https://stackoverflow.com/questions/24887158/wordpress-acf-get-field-not-returning-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!