查看: 90|回复: 0

[教程] Wordpress 显示主题图片的实现代码

[复制链接]

2

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-3-31
发表于 2023-8-21 00:00:00 | 显示全部楼层 |阅读模式

代码如下:


<?php 

//Put this in functions.php 

function get_primary_image($id, $size){ 
$featured = wp_get_attachment_image_src( get_post_thumbnail_id($id), $size, false); 
if($featured){ 
$childURL = $featured['0']; 
}else{ 
$children = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1)); 
reset($children); 
$childID = key($children); 
//$childURL = wp_get_attachment_url($childID); 
$childArray = wp_get_attachment_image_src($childID, $size, false); 
$childURL = $childArray['0']; 
if(empty($childURL)){ 
$childURL = get_bloginfo('template_url')."/images/default.png"; 


return($childURL); 



//Run this in the loop (or any place you'd like - as long as you have an ID to feed it..) 
//First argument is the ID.. 
//Second argument is the size.. It'll handle 'large', 'medium', 'thumbnail' or even 

'array(100, 100)'.. 
get_primary_image(get_the_ID(), 'large'); 

?> 

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部