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

dvl*_*963 4 php wordpress image image-gallery custom-wordpress-pages

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

$image_alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true); 
Run Code Online (Sandbox Code Playgroud)

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

$image_description = get_post_meta($image_id, '_wp_attachment_description', true);
$image_caption = get_post_meta($image_id, '_wp_attachment_caption', true);
Run Code Online (Sandbox Code Playgroud)

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

我正在尝试使用特色图像Alt TextTitleCaptionDescription来改进我的网站 SEO,但我不明白为什么它们显示为空。

在此输入图像描述

我找到了这篇文章这篇文章,但它们让我更加困惑。

你能帮我一下吗?

先感谢您。

Ruv*_*vee 10

“这两个人空手而归”

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


第一种方式

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

在此输入图像描述在此输入图像描述

现在为了获取您正在寻找的数据,您可以使用attachment_url_to_postidget_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);
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)

输出如下:

在此输入图像描述

笔记:

  • 我使用图像的虚拟链接只是为了给您一个示例,因此请务必将其更改为您想要获取其元数据的图像!
  • 正如您在屏幕截图中看到的,图像描述存储为post_content,图像标题存储为post_excerpt
  • 我还为你准备了post_type和!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();
Run Code Online (Sandbox Code Playgroud)

输出如下:

在此输入图像描述


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

在上面的两个解决方案中,我们都在重复自己。因此,为了遵循“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;
}
Run Code Online (Sandbox Code Playgroud)

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

$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>";
Run Code Online (Sandbox Code Playgroud)

或者在 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>";
Run Code Online (Sandbox Code Playgroud)

输出这个关联数组:

在此输入图像描述


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


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

  • 很好的答案!!!! (3认同)
  • 哇!工作起来就像一个魅力。老实说,我没想到会有这么详细的解释。太感谢了。我总是在 stackoverflow 上找到很棒的帮助。您的回答使我能够挑选任何元数据片段或删除我不需要的元数据片段,这非常棒。我特别喜欢可重用的功能。谢谢。 (2认同)