Advanced Custom Fields - Wordpress

2019-05-14 09:38发布

While using the Custom Fields Plugin, I cannot get it to return any data.

I have created a field group called book_cover_thumbnail which has one post linked to it. Can anyone see why the code below would not work?

<img src="<?php get_field('book_cover_thumbnail');?>" />

I get no errors at all, no white space.

3条回答
Summer. ? 凉城
2楼-- · 2019-05-14 10:29

Make sure you are a) Echoing the field using either the_field() or echo get_field(), and b) this code is either within the wordpress loop like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>`
<img src="<?php echo get_field('book_cover_thumbnail');?>" />
<?php end while; endif; ?>`

Or you add the post id to the get_field() function as a parameter:

$post_id = *your_post_ID_here*;
<img src="<?php echo get_field('book_cover_thumbnail', $post_id);?>" />

Documentation:

Using get_field(): http://www.advancedcustomfields.com/resources/functions/get_field/

Using the_field(): http://www.advancedcustomfields.com/resources/functions/the_field/

All Advanced Custom Fields Documentation: http://www.advancedcustomfields.com/resources/

查看更多
对你真心纯属浪费
3楼-- · 2019-05-14 10:31

Little bit late but also important nevertheless:

You can change the "Return Format" in Custom Fields -> Field Groups -> Return Format You have the choice between value/label and both(array)

Maybe that could help you in this case

查看更多
再贱就再见
4楼-- · 2019-05-14 10:33

Change get_field to the_field. Get field returns the value but doesn't echo it.

Alternatively, put an echo in front of the get field.

查看更多
登录 后发表回答