发布时间:2020-09-17
WordPress 技巧:获取特色图片地址,
我们知道 WordPress 可以在后台设置一张特色图片作为日志缩略图,但是我们如何获取这张特色图片的地址呢?
<?php
/*
Plugin Name: 获取 WordPress 特色图片地址
Plugin URI: http://blog.wpjam.com/m/get_post_thumbnail_url/
Description: 获取 WordPress 特色图片地址。
Version: 0.1
Author: Denis
Author URI: http://blog.wpjam.com/
*/
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;
}
}
[/code]
将上面的代码复制到当前主题的 functions.php 或者单独保存为一个插件并上传激活。使用下面方法调用:
[code]
$post_thumbnail_url = get_post_thumbnail_url($post->ID);
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
本站推荐使用的主机:,国外主机建议使用。
全站搜索