“这两个人空手而归”
因为在 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
并且工作正常!