我正在嘗試獲取與特色影像關聯的元資料,但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和Description的特色圖片,以提高我的網站SEO,但我不明白,為什么他們來了空。

我找到了

現在為了獲得您正在尋找的資料,您可以使用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);
為了測驗它,我們可以這樣做:
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>
輸出這個:

筆記:
- 我使用了一個指向影像的虛擬鏈接只是為了給你一個例子,所以一定要把它改成你想要獲取元資料的影像!
- 正如您在螢屏截圖中看到的,影像描述存盤為
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();
輸出這個:

第三種方式,稍微重構和優化我們的代碼!
在上面的兩個解決方案中,我們都在重復自己。因此,為了遵循“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并且作業正常!
uj5u.com熱心網友回復:
標題實際上是POST Excerpt,描述實際上是POST Content
所以你會這樣做:
/* 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 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/342494.html
標籤:php WordPress的 图片 自定义wordpress-pages 图片库
下一篇:從影像張量復制片段
