Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced Custom Fields - Wordpress

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.

like image 592
2ne Avatar asked Dec 07 '25 21:12

2ne


2 Answers

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/

like image 166
Nate F. Avatar answered Dec 09 '25 14:12

Nate F.


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.

like image 43
Owen C. Jones Avatar answered Dec 09 '25 14:12

Owen C. Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!