我正在wordpress的一个网站上工作,我正试图从一篇文章的高级自定义字段中获取缩略图,该字段如下:
<?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. ?>
问题是,当我加载网站时,图像得到的url看起来像这样:
<img src="5, , item_alt2, , , http://portfolio.local/wp-content/uploads/2012/09/item_alt2.jpg, Array">
所以,就我所见,它正在工作的插件会查找图像,但它加载了一条奇怪的路径,所以它显示的图像像"坏了"。
我做错了什么?
5, , item_alt2, , , http://portfolio.local/wp-content/uploads/2012/09/item_alt2.jpg, ArrayNULL
查看高级自定义字段上的文档;
http://www.advancedcustomfields.com/docs/field-types/image/
根据底部的示例,您将自定义字段的值作为对象返回;
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
)
)
我也遇到了同样的问题。你所需要做的就是确保你没有将图像作为对象返回。您可以在创建/编辑自定义字段时执行此操作。只需确保将返回值设置为图像url:
https://www.evernote.com/shard/s3/sh/91d181ed-e9d6-4504-8ab5-cee7ae85d84f/dfc9944d16337e41fb6e7e30ed26bdb5
然后,您应该能够使用它在模板中显示图像:
是否将customfield设置为返回url。看起来它被设置为对象。关于如何返回图像,您有三个选项。
或者您可以将自定义字段设置为IMAGE ID并像一样使用它
$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. ?>