WordPress 技巧:获取特色图片地址

WordPress 已经提供原生的获取特色图片地址的函数:get_the_post_thumbnail_url

我们知道 WordPress 可以在后台设置一张特色图片作为日志缩略图,但是我们如何获取这张特色图片的地址呢?

<?php 
/*
Plugin Name: 获取 WordPress 特色图片地址
*/
function get_post_thumbnail_url($post_id){
	$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
	$thumbnail_id = get_post_thumbnail_id($post_id);
	if($thumbnail_id ){
		$thumb = wp_get_attachment_image_src($thumbnail_id, 'thumbnail');
		return $thumb[0];
	}else{
		return false;
	}
}

将上面的代码复制到当前主题的 functions.php 或者单独保存为一个插件并上传激活。使用下面方法调用:

$post_thumbnail_url = get_post_thumbnail_url($post->ID);

🏆 每日挑战:你知道答案吗?

什么是 CSS 的 `pointer-events` 属性?

温馨提示 : 非特殊注明,否则均为©李联华的博客网原创文章,本站文章未经授权禁止任何形式转载;IP地址:18.97.9.168,归属地:弗吉尼亚州Ashburn ,欢迎您的访问!
文章链接:https://www.lilianhua.com/wordpress-tip-get-the-featured-picture-address.html

文澜千文文澜千文Beta

请登录以使用此功能。

弹窗图片

年终回馈! 200积分免费领取,可通过用户中心或VIP会员页面通过兑换码进行兑换:OAK-8909956

Loading...