外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>基础教程>WordPress 通过简码调用附加到文章的最后一张图片

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

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

/**
 * 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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
基础教程

WordPress 站点集成 Google 自定义搜索引擎

2014-3-11 10:34:58

基础教程

WordPress为页面(page)添加相关页面

2014-3-15 9:17:12

3 条回复 A文章作者 M管理员
  1. 昨天刚看了调用第一张图片。。。

  2. 超級efly

    支持一下~不過「最後一張」到底有什麼用處。。。

  3. 这个能用来干啥呢-,-

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索