一个分享WordPress、Zblog、Emlog、Typecho等主流博客的教程网站!
当前位置:网站首页 > WordPress教程 > 正文

WordPress 通过简码调用附加到文章的最后一张图片

作者:xlnxin发布时间:2021-04-15分类:WordPress教程浏览:438


导读:WordPress的简码是一个非常简单易用的功能,之...

WordPress的简码是一个非常简单易用的功能,之前我们已经分享了 WordPress Shortcode(简码)介绍及使用详解,今天我们一起来看看,WordPress 如何通过简码调用附加到文章的最后一张图片。方法很简单,只需要在当前主题的 functions.php 添加下面的代码:

123456789101112131415161718
/** * WordPress 通过简码调用附加到文章的最后一张图片 * https://www.wpdaxue.com/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");

然后我们在文章中使用下面的简码就可以调用文章的最后一张图片啦:

[postimage]

参考资料:wprecipes.com

标签:wordpresshtmldiv主题