【问题标题】:Url and alt from a image in wordpress来自wordpress中图像的Url和alt
【发布时间】:2020-04-18 16:44:21
【问题描述】:
$hover = get_post_meta( $cliente->ID, 'hover', true );

hover 是一个带有 ACF 的自定义帖子字段。 这个自定义帖子字段有一张图片

$hover 有一个输出:string(3) "694"

如何从这张图片中获取 url 和 alt?

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    您需要使用wp_get_attachement 函数。

    $hoverURL = wp_get_attachment_url( (int)$hover );
    $hoverALT = get_post_meta( (int)$hover, '_wp_attachment_image_alt', true );
    

    ALT 内容保存为图片附件的元数据,因此您仍在使用标准get_post_meta()。我还使用 (int)$hover 从字符串转换为整数,因为这些 WP 函数要求 id 是整数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 2019-07-22
      • 2012-11-25
      相关资源
      最近更新 更多