高级自定义字段缩略图 - wordpress

时间:2012-09-08 17:59:45

标签: php wordpress field

我正在使用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. ?>

问题在于,当我加载网站时,图片的网址看起来像这样:

<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

4 个答案:

答案 0 :(得分:7)

查看高级自定义字段的文档;

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
    )

)

答案 1 :(得分:0)

我有同样的问题。您需要做的就是确保不将图像作为对象返回。您可以在创建/编辑自定义字段时执行此操作。只需确保将返回值设置为image url:

https://www.evernote.com/shard/s3/sh/91d181ed-e9d6-4504-8ab5-cee7ae85d84f/dfc9944d16337e41fb6e7e30ed26bdb5

然后,您应该可以使用它来在模板中显示图像:

答案 2 :(得分:0)

您是否将自定义字段设置为返回网址。看起来它设置为对象。您有3种选择如何返回图像。

答案 3 :(得分:0)

或者您可以将自定义字段设置为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. ?>
相关问题