【问题标题】:How to get the dynamic image title from wordpress from "get_the_post_thumbnail" function?如何通过“get_the_post_thumbnail”功能从wordpress获取动态图片标题?
【发布时间】:2021-09-06 14:26:52
【问题描述】:

我正在学习编码,并且在开发测试网站时,我喜欢下面的代码 sn-p 工作的方式来获得更多控制和打印响应式 srcset 图像。唯一缺少的是如何获取我在 wordpress 媒体上传中添加的动态标题,以作为代码的结果显示在前端。

请注意,下面的 sn-p 可以正确打印图像中的 alt 标签,但是,标题没有显示...

<?php
    echo get_the_post_thumbnail($post->ID, 'large', array('title' => '', 'sizes' => '(max-width: 480px) 480px, (max-width: 640px) 512px, (max-width: 960px) 960px, (max-width: 1280px) 480px, '));
?>

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages responsive-images


    【解决方案1】:

    一种方法是使用 get_post 并传递缩略图的 ID 并从对象中获取标题。

    echo get_post($post->ID)->post_title;
    echo get_post(get_post_thumbnail_id())->post_title;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 2016-10-14
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多