【问题标题】:Advanced Custom field thumbnail - wordpress高级自定义字段缩略图 - wordpress
【发布时间】:2012-09-02 05:26:10
【问题描述】:

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

【问题讨论】:

  • 只是看着它,并不太了解 Wordpress,您使用 the_field('thumbnail') 调用它的方式是以字符串形式返回一个逗号分隔的列表,带有缩略图 可能 在索引 #5 处(零是第一个)。所以要么你向函数传递了错误的值,要么你应该以某种方式解析它($thumb = explode(', ', the_field('thumbnail'));,然后使用$thumb[5]),或者类似的东西。
  • the_field('thumbnail') 是高级自定义字段插件功能的字段调用方式
  • 调用the_field('custom_title'); 是否返回正确的值,即“我的自定义标题”?
  • 是的,“the_field('custom_title');”返回我在帖子自定义字段框中写的自定义标题。
  • 你真的需要看看你得到了什么,并试着弄清楚你为什么得到它。您还应该有来源,因此参考它并找出您获得特定结果的原因也应该是富有成效的。您can get the url from the returned function call,但这似乎需要做很多工作,所以请先检查您是否做对了。

标签: php wordpress field


【解决方案1】:

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

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
    )

)

【讨论】:

  • 完全有意义(在文档中没有看到),当然可以理解打印的值。
  • @JaredFarrish - 我知道,文本几乎全部变灰无济于事!看起来像是在更高版本的 ACF 3.3.7+ 中添加的东西
  • 是的,它有效哈哈,非常感谢!只需更改为返回 URL 而不是对象!
  • 不用担心 - 杰瑞德和你做了所有的努力 :) 对那个代表感到有点内疚....但没那么内疚 ;)
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    您是否将自定义字段设置为返回 url。看起来它被设置为对象。您有 3 个选项来返回图像。

    【讨论】:

    • 欢迎来到stackoverflow!请注意,这可能应该是评论而不是答案。顺便问一下,返回图像的 3 个选项是什么?
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2012-08-10
      • 2015-01-21
      • 2017-05-01
      相关资源
      最近更新 更多