无所屌谓 發表於 2023-8-21 00:00:00

Wordpress 显示主题图片的实现代码

<p>
代码如下:</p>
<p>
<br>
&lt;?php <br><br>
//Put this in functions.php <br><br>
function get_primary_image($id, $size){ <br>
$featured = wp_get_attachment_image_src( get_post_thumbnail_id($id), $size, false); <br>
if($featured){ <br>
$childURL = $featured['0']; <br>
}else{ <br>
$children = get_children(array('post_parent' =&gt; $id, 'post_type' =&gt; 'attachment', 'post_mime_type' =&gt; 'image', 'numberposts' =&gt; 1)); <br>
reset($children); <br>
$childID = key($children); <br>
//$childURL = wp_get_attachment_url($childID); <br>
$childArray = wp_get_attachment_image_src($childID, $size, false); <br>
$childURL = $childArray['0']; <br>
if(empty($childURL)){ <br>
$childURL = get_bloginfo('template_url')."/images/default.png"; <br>
} <br>
} <br>
return($childURL); <br>
} <br><br><br>
//Run this in the loop (or any place you'd like - as long as you have an ID to feed it..) <br>
//First argument is the ID.. <br>
//Second argument is the size.. It'll handle 'large', 'medium', 'thumbnail' or even <br><br>
'array(100, 100)'.. <br>
get_primary_image(get_the_ID(), 'large'); <br><br>
?&gt; </p>
頁: [1]
查看完整版本: Wordpress 显示主题图片的实现代码