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 Text的Title、Caption和Description来改进我的网站 SEO,但我不明白为什么它们显示为空。
你能帮我一下吗?
先感谢您。
Ruv*_*vee 10
“这两个人空手而归”
因为在 post 元表中没有名为 的键'_wp_attachment_description'。同样的事情与'_wp_attachment_caption'. 它们存储在 posts 表中。这就是为什么get_post_meta返回空!
第一种方式
举例来说,我上传了 WordPress 的徽标之一并填充这些元数据字段,如下所示:
现在为了获取您正在寻找的数据,您可以使用attachment_url_to_postid和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);
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并且工作正常!