【问题标题】:How to get an image metadata such as alt text, caption and description from media library in wordpress如何从 wordpress 的媒体库中获取图像元数据,例如 alt 文本、标题和描述
【发布时间】:2021-12-14 02:51:11
【问题描述】:

我正在尝试获取与特色图片关联的元数据,但 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 TextTitleCaptionDescription 来改善我的网站 SEO,但我不知道为什么它们会空出来。

我找到了this postthis post,但它们让我更加困惑。

你能帮帮我吗?

提前谢谢你。

【问题讨论】:

    标签: php wordpress image custom-wordpress-pages image-gallery


    【解决方案1】:

    “这两个返回空”

    因为在 post 元表中没有名为 '_wp_attachment_description' 的键。与'_wp_attachment_caption' 相同。它们存储在帖子表中。这就是 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);
    

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

    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_typemime_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 上进行了全面测试,并且运行良好!

    【讨论】:

    • 不错的答案!!!
    • 哇!像魅力一样工作。老实说,我没想到会有这么详细的解释。太感谢了。我总是在 stackoverflow 上找到很棒的帮助。您的回答使我能够挑选任何元数据片段或删除我不需要的元数据,这太棒了。我特别喜欢可重复使用的功能。谢谢。
    • 嗨@Ruvee!我有一个后续问题。我想知道是否有一种方法可以在简码中使用该功能。谢谢。
    【解决方案2】:

    标题实际上是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 );
    

    【讨论】:

    • 感谢您的帮助,我会支持您的回答。你的答案有效。
    猜你喜欢
    • 1970-01-01
    • 2022-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2015-02-26
    • 2011-09-01
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多