[英]How to get an image metadata such as alt text, caption and description from media library in wordpress
I am trying to get metadata associated with the featured image but get_post_meta
keeps returning empty.我正在尝试获取与特色图像关联的元数据,但get_post_meta
一直返回为空。
$image_alt_text = get_post_meta($image_id, '_wp_attachment_image_alt', true);
This works and returns the data, but the code below does not work:这有效并返回数据,但下面的代码不起作用:
$image_description = get_post_meta($image_id, '_wp_attachment_description', true);
$image_caption = get_post_meta($image_id, '_wp_attachment_caption', true);
These two return empty.这两个返回空。 I filled out those fields but I can not get them back in my template!我填写了这些字段,但无法将它们恢复到我的模板中!
I am trying to use Alt Text
, Title
, Caption
and Description
of the featured image to improve my website SEO, but I can not figure out why they're coming out empty.我正在尝试使用特色图像的Alt Text
、 Title
、 Caption
和Description
来改进我的网站 SEO,但我不知道为什么它们会出现空的。
I found this post and this post , but they make me even more confused.我找到了这篇文章和这篇文章,但它们让我更加困惑。
Would you please help me?你能帮我吗?
Thank you in advance.先感谢您。
"These two return empty" “这两个返回空”
Because in the post meta table there is no key called '_wp_attachment_description'
.因为在 post meta 表中没有名为'_wp_attachment_description'
键。 Same thing with '_wp_attachment_caption'
.与'_wp_attachment_caption'
相同。 They're stored in the posts table.它们存储在posts 表中。 That's why get_post_meta
returns empty!这就是get_post_meta
返回空的原因!
First way第一种方式
So let's say,for example, I upload one of the wordpress's logo and populate those metadata fields, like this:例如,假设我上传了 wordpress 的徽标之一并填充这些元数据字段,如下所示:
Now in order to get the data you're looking for you could use attachment_url_to_postid
and get_post_field
functions.现在为了获得您正在寻找的数据,您可以使用attachment_url_to_postid
和get_post_field
函数。 So you could do something like this:所以你可以做这样的事情:
$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);
In order to test it, we could do something like this:为了测试它,我们可以这样做:
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>
Which outputs this:输出这个:
NOTE:笔记:
post_content
and image caption is stored as post_excerpt
.正如您在屏幕截图中看到的,图像描述存储为post_content
,图像标题存储为post_excerpt
。post_type
and mime_type
for you!我还为您准备了post_type
和mime_type
!Second way, in the wordpress loop第二种方式,在wordpress循环中
If you want to get the metadata in the wordpress loop, we could use get_post_thumbnail_id
function.如果你想在 wordpress 循环中获取元数据,我们可以使用get_post_thumbnail_id
函数。 So you could use the following code:所以你可以使用以下代码:
$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();
Which outputs this:输出这个:
Third way, refactoring and optimizing our code a little bit!第三种方式,稍微重构和优化我们的代码!
In both solutions above, we're repeating ourselves.在上面的两个解决方案中,我们都在重复自己。 So to follow "DRY" principle, we could write a reusable function which will return an associative array of the metadata related to an image id.因此,为了遵循“DRY”原则,我们可以编写一个可重用的函数,该函数将返回与图像 ID 相关的元数据的关联数组。 Like so:像这样:
This function goes into the functions.php
file of your theme.这个函数进入你的主题的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;
}
Now in order to use this function in your templates, you could do something like this:现在为了在你的模板中使用这个函数,你可以这样做:
$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>";
Or in the wordpress loop:或者在 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>";
Which outputs this associative array:它输出这个关联数组:
Hope this answer clears things up for you!希望这个答案可以为您解决问题!
This answer has been fully tested on wordpress 5.8
and works fine!这个答案已经在 wordpress 5.8
上进行了全面测试并且工作正常!
The Caption is actually the POST Excerpt
and the Description is actually the POST Content
Caption 实际上是POST Excerpt
,Description 实际上是POST Content
So you would do this:所以你会这样做:
/* int - the attachment id */
$image_data = get_post( int );
$description = apply_filters( 'the_content', $image_data->post_content );
$caption = apply_filters( 'the_excerpt', $image_data->post_excerpt );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.