WordPress的簡(jiǎn)碼是一個(gè)非常簡(jiǎn)單易用的功能,之前我們已經(jīng)分享了 WordPress Shortcode(簡(jiǎn)碼)介紹及使用詳解,今天我們一起來看看,WordPress 如何通過簡(jiǎn)碼調(diào)用附加到文章的最后一張圖片。方法很簡(jiǎn)單,只需要在當(dāng)前主題的 functions.php 添加下面的代碼:
/**
* WordPress 通過簡(jiǎn)碼調(diào)用附加到文章的最后一張圖片
* http://www.ydqwiac.cn/wordpress-shortcode-display-the-last-image-attached-to-post.html
*/
function wpdx_postimage($atts, $content = null) {
extract(shortcode_atts(array(
"size" => 'thumbnail',
"float" => 'none'
), $atts));
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
foreach( $images as $imageID => $imagePost )
$fullimage = wp_get_attachment_image($imageID, $size, false);
$imagedata = wp_get_attachment_image_src($imageID, $size, false);
$width = ($imagedata[1]+2);
$height = ($imagedata[2]+2);
return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';
}
add_shortcode("postimage", "wpdx_postimage");
然后我們?cè)谖恼轮惺褂孟旅娴暮?jiǎn)碼就可以調(diào)用文章的最后一張圖片啦:
[postimage]
參考資料:wprecipes.com
聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。





昨天剛看了調(diào)用第一張圖片。。。
支持一下~不過「最後一張」到底有什麼用處。。。
這個(gè)能用來干啥呢-,-