如何从 WordPress 媒体库中获取图像元数据,例如替代文本、标题和描述

2024-01-10

我正在尝试获取与特色图像相关的元数据,但是get_post_meta不断返回空。

$image_alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true); 

这可以工作并返回数据,但下面的代码不起作用:

$image_description = get_post_meta($image_id, '_wp_attachment_description', true);
$image_caption = get_post_meta($image_id, '_wp_attachment_caption', true);

这两个返回空。我填写了这些字段,但无法将它们返回到我的模板中!

我正在尝试使用Alt Text, Title, Caption and Description的特色图像来改善我的网站搜索引擎优化,但我不明白为什么它们是空的。

I found 这个帖子 https://stackoverflow.com/questions/20664356/get-a-single-specific-image-from-wordpress-media-library and 这个帖子 https://stackoverflow.com/questions/65614823/add-image-title-and-or-description-to-the-caption,但它们让我更加困惑。

你能帮我一下吗?

先感谢您。


“这两个人空手而归”

因为在 post 元表中没有名为'_wp_attachment_description'。同样的事情与'_wp_attachment_caption'。它们存储在 posts 表中。这就是为什么get_post_meta返回空!


第一种方式

举例来说,我上传了 WordPress 的徽标之一并填充这些元数据字段,如下所示:

现在为了获取您正在寻找的数据,您可以使用attachment_url_to_postid and get_post_field功能。所以你可以这样做:

$image_id = attachment_url_to_postid( 'http://yourwebsite.dev/wp-content/uploads/2021/10/WordPress-logotype-alternative-white.png' );  

$image_alt_text        = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_caption         = get_post_field('post_excerpt', $image_id);
$image_title           = get_post_field('post_title', $image_id);
$image_content         = get_post_field('post_content', $image_id);
$image_name            = get_post_field('post_name', $image_id);
$image_post_type       = get_post_field('post_type', $image_id);
$image_post_mime_type  = get_post_field('post_mime_type', $image_id);

为了测试它,我们可以这样做:

echo "<strong>This is the id of the featured image:</strong><span style='color:green;'> {$image_id}</span>";
echo '<br>';
echo "<strong>This is the alternative Text:</strong><span style='color:green;'> {$image_alt_text}</span>";
echo '<br>';
echo "<strong>This is the caption:</strong><span style='color:green;'> {$image_caption}</span>";
echo '<br>';
echo "<strong>This is the title:</strong><span style='color:green;'> {$image_title}</span>";
echo '<br>';
echo "<strong>This is the description of the image:</strong><span style='color:green;'> {$image_content}</span>";
echo '<br>';
echo "<strong>This is the original name of the file:</strong><span style='color:green;'> {$image_name}</span>";
echo '<br>';
echo "<strong>This is the post type:</strong><span style='color:green;'> {$image_post_type}</span>";
echo '<br>';
echo "<strong>This is the mime type of the image:</strong><span style='color:green;'> {$image_post_mime_type}</span>";
echo '<br>

输出如下:

NOTE:

  • 我使用图像的虚拟链接只是为了给您一个示例,因此请务必将其更改为您想要获取其元数据的图像!
  • 正如您在屏幕截图中看到的,图像描述存储为post_content图像标题存储为post_excerpt.
  • 我也得到了post_type and mime_type为你!

第二种方式,在wordpress循环中

如果你想获取wordpress循环中的元数据,我们可以使用get_post_thumbnail_id功能。所以你可以使用下面的代码:

$args = array(
  'post_type' => 'post',
  'posts_per_page' => -1,
);

$query = new WP_Query($args);

if($query){
  while($query->have_posts()){
    $query->the_post();
    echo "<strong>This is the title of a post: </strong>" . get_the_title();
    $image_id = get_post_thumbnail_id(get_the_id());
    echo '<br>';
    echo "<strong>This is the id of the featured image:</strong><span style='color:green;'> {$image_id}</span>";
    echo '<br>';
    $image_alt_text        = get_post_meta($image_id, '_wp_attachment_image_alt', true);
    $image_caption         = get_post_field('post_excerpt', $image_id);
    $image_title           = get_post_field('post_title', $image_id);
    $image_content         = get_post_field('post_content', $image_id);
    $image_name            = get_post_field('post_name', $image_id);
    $image_post_type       = get_post_field('post_type', $image_id);
    $image_post_mime_type  = get_post_field('post_mime_type', $image_id);

    echo "<strong>This is the alternative Text:</strong><span style='color:green;'> {$image_alt_text}</span>";
    echo '<br>';
    echo "<strong>This is the caption:</strong><span style='color:green;'> {$image_caption}</span>";
    echo '<br>';
    echo "<strong>This is the title:</strong><span style='color:green;'> {$image_title}</span>";
    echo '<br>';
    echo "<strong>This is the description of the image:</strong><span style='color:green;'> {$image_content}</span>";
    echo '<br>';
    echo "<strong>This is the original name of the file:</strong><span style='color:green;'> {$image_name}</span>";
    echo '<br>';
    echo "<strong>This is the post type:</strong><span style='color:green;'> {$image_post_type}</span>";
    echo '<br>';
    echo "<strong>This is the mime type of the image:</strong><span style='color:green;'> {$image_post_mime_type}</span>";
  }
}

wp_reset_postdata();

输出如下:


第三种方法,稍微重构和优化我们的代码!

在上面的两个解决方案中,我们都在重复自己。因此,为了遵循“DRY”原则,我们可以编写一个可重用的函数,它将返回与图像 id 相关的元数据的关联数组。就像这样:

该函数进入functions.php您的主题文件。

/**
 * A function to retrieve corresponding metadata for an image uploaded through Media Library in Wordpress
 *
 * @author https://stackoverflow.com/users/15040627/ruvee
 * 
 * @param string|int The id of the image you're trying to get metadata for!
 * 
 * @return array This function returns an associative array of metadata related to the image id being passed to it.
 */

function getting_image_metadata($image_id){

  $image_metadata_array = array();

  $image_metadata_array['image_alt_text']        = get_post_meta($image_id, '_wp_attachment_image_alt', true) ?? '';
  $image_metadata_array['image_caption']         = get_post_field('post_excerpt', $image_id) ?? '';
  $image_metadata_array['image_title']           = get_post_field('post_title', $image_id) ?? '';
  $image_metadata_array['image_content']         = get_post_field('post_content', $image_id) ?? '';
  $image_metadata_array['image_name']            = get_post_field('post_name', $image_id) ?? '';
  $image_metadata_array['image_post_type']       = get_post_field('post_type', $image_id) ?? '';
  $image_metadata_array['image_post_mime_type']  = get_post_field('post_mime_type', $image_id) ?? '';

  return $image_metadata_array;
}

现在,为了在模板中使用此函数,您可以执行以下操作:

$image_id = attachment_url_to_postid( 'http://yourwebsite.dev/wp-content/uploads/2021/10/WordPress-logotype-alternative-white.png' );

$image_metadata_array = getting_image_metadata($image_id);

echo "<pre>";
print_r($image_metadata_array);
echo "</pre>";

或者在 wordpress 循环中:

$image_id = get_post_thumbnail_id(get_the_id()); 

$image_metadata_array = getting_image_metadata($image_id);

echo "<pre>";
print_r($image_metadata_array);
echo "</pre>";

输出这个关联数组:


希望这个答案能为您解决问题!


这个答案已经在 wordpress 上经过充分测试5.8并且工作正常!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 WordPress 媒体库中获取图像元数据,例如替代文本、标题和描述 的相关文章

  • 如何在html中制作多行类型的文本框?

  • Zend Framework Zend_Form 装饰器: 位于按钮元素内部?

    我有一个像这样创建的按钮元素 submit new Zend Form Element Button submit submit gt setLabel My Button submit gt setDecorators array Vie
  • php下拉菜单人口

    我正在尝试编写一个 php 脚本 该脚本将根据主下拉菜单的选择填充第二个下拉菜单 我想使用 jquery 来完成所有非页面刷新的事情 但我发现现有的所有东西都很难理解和修改 你知道有什么写得很好且易于理解的东西吗 或者可能是现有的教程 下面
  • 如何在 codeigniter 查询中使用 FIND_IN_SET?

    array array classesID gt 6 this gt db gt select gt from this gt table name gt where array gt order by this gt order by q
  • 检查文件权限

    我怎样才能检查file permissions 无需通过运行操作系统特定命令passthru or exec Use 文件权限 http php net fileperms功能 clearstatcache echo substr spri
  • PHP - 扩展 __construct

    我想知道你是否可以帮助我 我有两个类 一个扩展了另一个 B 类将由各种不同的对象扩展 并用于常见的数据库交互 现在我希望 B 类能够处理其连接和断开连接 而无需来自 A 类或任何外部输入的指示 据我了解 问题是扩展类不会自动运行其 cons
  • jquery上传完成后重定向到新页面

    我正在尝试让这个 jquery 工具与我的网站一起使用以进行文件上传 https github com blueimp jQuery File Upload https github com blueimp jQuery File Uplo
  • PHP、jQuery 和 Ajax 调用乱序

    我正在使用 jQuery 进行 Ajax 调用 我有 x 数量的 Ajax 调用附加到 div 这些 Ajax 加载请求是由 PHP foreach 循环生成的 问题是它们渲染的顺序不正确 它们被设置在数组中
  • 将数据库中的用户 ID 添加到 Codeigniter 中的会话数据中?

    我是 CodeIgniter 的新手 在从数据库添加用户 ID 用户登录后 到会话数据时遇到问题 这是我的代码问题 之前可能会在 SOF 上被问到 在付出了所有努力之后 我问这个 登录模型
  • PHP 基本身份验证 file_get_contents() [重复]

    这个问题在这里已经有答案了 我需要从网站解析一些 XML 数据 XML 数据是原始格式 但在我需要进行身份验证之前 基于基本网络服务器的身份验证 使用用户名和密码 I tried homepage file get contents htt
  • Doctrine 1 和 Symfony 1 的多个主键?

    我已经知道在 Symfony 1 和 Doctrine 1 中不可能使用多个主键 但是你们知道有什么好的解决方法吗 除了多对多关系之外 原则 1 不适用于多列上的主键 但如果你想使用多对多关系 请像这样使用 BlogPost columns
  • 删除 woocommerce 店面主页标题 php

    我正在使用 woocommerce 的店面主题 我需要用 php 删除主页标题 h1 我知道 css 解决方案 但我不想使用它 因为我想将 h1 添加到该页面的其他位置 并且在一个页面中包含 2 个 h1 对 seo 不利页 我也知道删除页
  • 具有更改用户代理上下文的 file_get_contents 不起作用

    我正在尝试获取页面的阅读数和点赞数 网址是 https mp weixin qq com s NPavBeHc8VdWXeSL6kfLRg https mp weixin qq com s NPavBeHc8VdWXeSL6kfLRg 您必
  • 如何解决 Laravel 8 UI 分页问题?

    我在尝试最近发布的 laravel 8 时遇到了问题 我试图找出变化是什么以及它是如何工作的 当我这样做时 我遇到了分页 laravel 8 UI 变得混乱的问题 不知何故它发生了 有人可以帮助我吗 或者经历过同样的事情 像这样我在 lar
  • 如何让Gmail像加载进度条一样

    我想在页面的中心和顶部创建一个像 Gmail 一样的加载进度条 并适用于所有浏览器 这是基本代码
  • PHP 错误警告:参数 1 应该是引用

    我 熟悉 PHP 我的朋友的网站因错误而崩溃 Warning Parameter 1 to Some function name expected to be a reference value given in public html i
  • MySQL 数据库无法在 XAMPP for Mac 上启动

    突然我在 mac 上遇到了这个问题 我无法启动我的 MySQL 数据库 我只能启动 ProFTPD 和 Apache Web Server 这是应用程序日志 Starting all servers Starting MySQL Datab
  • UTF-8、PHP、Win7 - 现在是否有解决方案可以使用 php 在 Win 7 上保存 UTF-8 文件名?

    更新 只是为了不让您阅读所有内容 PHP 开头 7 1 0alpha2 在 Windows 上支持 UTF 8 文件名 感谢阿纳托尔 贝尔斯基 根据 stackoverflow 上的一些链接 我找到了部分答案 https stackover
  • magento成功页面变量

    我正在尝试捕获一些 magento 成功页面变量以传递给我们的广告公司 到目前为止 我已经得到了这个 但变量没有输出任何内容 数据需要采用以下格式 price1 price2 price3 qty1 qty2 qty3 sku1 sku2
  • 谷歌日历手表过期时间超过1个月怎么办?

    我将我的 CRM 系统与 Google 日历同步 为此 我要求push notifications从我的用户 Google 日历到我的系统 我已经成功创建了手表 并将过期时间设置为2030年 但是当我收到手表事件的响应时 它显示手表的过期时

随机推荐