WordPress 获取附件图片的标题、说明、代替文本、图像描述

导语:有一位客户的主题需要获取附件图片的代替文本,叶子开始在网上查了很久没有查到具体的函数,后来看wordpress开发文档的代码,发现了它使用get_post_meta()来获取的。

说明

get_post_meta 函数是用来取出自定义字段的值的函数,那么在附件图片中代替文本信息就是一个自定义的值。然后叶子以为图像描述也是自定义的值,结果在`wp_postmeta`没有发现。

SELECT * FROM `wp_postmeta` WHERE `post_id` =109

发现这张图片就三条信息。

338 109 _wp_attached_file 2016/05/wordpress-life_the_post_thumbnail_url_20160608.jpg
339 109 _wp_attachment_metadata a:5:{s:5:"width";i:1000;s:6:"height";i:750;s:4:"file";s:58:"2016/05/wordpress-life_the_post_thumbnail_url_20160608.jpg";s:5:"sizes";a:7:{s:9:"thumbnail";a:4:{s:4:"file";s:58:"wordpress-life_the_post_thumbnail_url_20160608-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:58:"wordpress-life_the_post_thumbnail_url_20160608-300x225.jpg";s:5:"width";i:300;s:6:"height";i:225;s:9:"mime-type";s:10:"image/jpeg";}s:12:"medium_large";a:4:{s:4:"file";s:58:"wordpress-life_the_post_thumbnail_url_20160608-768x576.jpg";s:5:"width";i:768;s:6:"height";i:576;s:9:"mime-type";s:10:"image/jpeg";}s:26:"codilight_lite_block_small";a:4:{s:4:"file";s:56:"wordpress-life_the_post_thumbnail_url_20160608-90x60.jpg";s:5:"width";i:90;s:6:"height";i:60;s:9:"mime-type";s:10:"image/jpeg";}s:29:"codilight_lite_block_1_medium";a:4:{s:4:"file";s:58:"wordpress-life_the_post_thumbnail_url_20160608-250x170.jpg";s:5:"width";i:250;s:6:"height";i:170;s:9:"mime-type";s:10:"image/jpeg";}s:29:"codilight_lite_block_2_medium";a:4:{s:4:"file";s:58:"wordpress-life_the_post_thumbnail_url_20160608-325x170.jpg";s:5:"width";i:325;s:6:"height";i:170;s:9:"mime-type";s:10:"image/jpeg";}s:28:"codilight_lite_single_medium";a:4:{s:4:"file";s:58:"wordpress-life_the_post_thumbnail_url_20160608-700x350.jpg";s:5:"width";i:700;s:6:"height";i:350;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}
340 109 _wp_attachment_image_alt 使用wordpress函数the_post_thumbnail_url()获取缩略图URL

后来明白了,附件图片其实也就是一篇特殊的文章,那么它的标题就是文章标题,图像描述就是文章内容,说明就是文章摘要。

具体代码

获取图片标题

get_the_title( $post_id)

获取图片的代替文本

get_post_meta( $post_id, '_wp_attachment_image_alt', true )

获取图片描述

get_the_content( $post_id)

获取图片说明

get_the_excerpt($post_id)

结束

你学会了吗


Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

Warning: copy(): Failed to enable crypto in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

Warning: copy(https://www.wordpressleaf.com/wp-content/themes/summerleaf/images/tx/46.png): failed to open stream: operation failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

目前在“WordPress 获取附件图片的标题、说明、代替文本、图像描述”上有2条评论


  1. Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

    Warning: copy(): Failed to enable crypto in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

    Warning: copy(https://www.wordpressleaf.com/wp-content/themes/summerleaf/images/tx/99.png): failed to open stream: operation failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546
    BanYuner说道:

    我学会了


    1. Warning: copy(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

      Warning: copy(): Failed to enable crypto in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546

      Warning: copy(https://www.wordpressleaf.com/wp-content/themes/summerleaf/images/tx/33.png): failed to open stream: operation failed in /opt/lampp/htdocs/wordpresssummer/wp-content/themes/summerleaf/inc/summer_setting.php on line 546
      编辑 杨说道:

      好的,新年好

发表评论

邮箱地址不会被公开。 必填项已用*标注