3

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 Answers 3

5

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/

Sign up to request clarification or add additional context in comments.

2 Comments

This src URL only works if you have set your custom field to output as the URL, and not an object or an ID.
@MikeKormendy correct. For an object you would have to access the specific key you'd like, in this case I'm guessing something like ['url'], or for a thumbnail sized image `['sizes']['thumb']['url']. For an ID, you'd have to use that ID to get the item you'd, most likely using a Wordpress function like wp_get_attachment($image_ID, 'thumbnail');, but without knowing the specific usage it's hard to say.
3

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.

7 Comments

I tried the field first and also <img src="<?php echo get_field('book_cover_thumbnail');?>" />
Must be a simple answer but I cannot find it
Are you outside of the WordPress loop? If so you need to give the field commands the post id so they can retrieve your field
maybe I am? I am not too sure. my post type is books, could you show me what my code should look like ?
I just tried copying this video vimeo.com/21468614 where at the start the guy populates all the fields onto the page, mine does not do this
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.