Reputation: 343
I'm working on a website in wordpress, i'm trying to get the thumbnail from the advanced custom field of a post with this:
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_field('custom_title'); ?></h1>
<img src="<?php the_field('thumbnail'); ?>" alt="" />
<p><?php the_content(); ?></p>
<?php endwhile; // end of the loop. ?>
The problem it's that when I load the website, the url the image gets looks like this:
<img src="5, , item_alt2, , , http://portfolio.local/wp-content/uploads/2012/09/item_alt2.jpg, Array">
So, as far as I see, the plugin it's working since looks for the image, but it loads an strange path to it, so it shows like "broken" image.
What i'm doing wrong?
5, , item_alt2, , , http://portfolio.local/wp-content/uploads/2012/09/item_alt2.jpg, ArrayNULL
Upvotes: 3
Views: 4393
Reputation: 135
Or you can just set custom field as IMAGE ID and use it like that
$image = wp_get_attachment_image_src(get_field('thumbnail'), 'sizename');
<h1><?php the_field('custom_title'); ?></h1>
<img src="' .$image[0]. '" alt="">
<p><?php the_content(); ?></p>
<?php endwhile; // end of the loop. ?>
Upvotes: 0
Reputation: 1
Have you set the customfield to return url. Looks lige it is set to object. You have 3 options to how to return an image.
Upvotes: 0
Reputation: 9316
I had this same problem. All you need to do is make sure you're not returning the image as an object. You do this when creating/editing the custom field. Just make sure you set the return value to image url:
Then, you should be able to use this to display the image in your template:
Upvotes: 0
Reputation: 6777
Have a look at the docs on Advanced Custom Fields;
http://www.advancedcustomfields.com/docs/field-types/image/
You are returning the value of the custom field as an object as per the example at the bottom;
Array
(
[id] => 540
[alt] => A Movie
[title] => Movie Poster: UP
[caption] => sweet image
[description] => a man and a baloon
[url] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
[sizes] => Array
(
[thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-150x150.jpg
[medium] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-300x119.jpg
[large] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
[post-thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
[large-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
[small-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-500x199.jpg
)
)
Upvotes: 7