打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
让wordpress文章列表页自动调用文章缩略图

wordpress有个功能,可以设置一篇文章的特色图像,这样在首页及各个列表页就可以利用the_post_thumbnail()这个函数来调用该图片的缩略图,而事实上我们在发博文的时候经常懒得多此一举(设置特色图像)。。其实懒人自有懒人的办法,下面这个小函数就可以实现自动调用文章的图片来作为缩略图而无需再设置特色图像(当然,如果一篇文章含有好几个图片又不想第一张图片作为缩略图的话,还是得手工设置一下)。
在外观–编辑里头找到functions.php,加入以下这个函数:

function emtx_auto_thumbnail($pID,$thumb='thumbnail') { $blogimg = FALSE;  if (has_post_thumbnail()) {// 判断该文章是否已经设置了“特色图像”,如果有则直接显示该特色图像的缩略图						$blogimg = wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb);						$blogimg = $blogimg[0]; } elseif ($postimages = get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0")) {//如果文章没有设置特色图像,则查找文章内是否有上传图片				foreach($postimages as $postimage) {							$blogimg = wp_get_attachment_image_src($postimage->ID, $thumb);							$blogimg = $blogimg[0];						}					} elseif (preg_match('/<img [^>]*src=["|\']([^"|\']+)/i', get_the_content(), $match) != FALSE) {						$blogimg = $match[1];					} 		if($blogimg) { $blogimg = '<a href="'. get_permalink().'"><img src="'.$blogimg.'" alt="'.get_the_title().'"  class="alignleft wp-post-image"  /></a>';}     	return $blogimg;  	} 

然后在相应的模板文件里面调用缩略图的地方做个修改,把原来调用the_post_thumbnail的地方按照实际需求改为诸如下面这样的代码即可:

<?php if(emtx_auto_thumbnail($post->ID) ) {   echo emtx_auto_thumbnail($post->ID);} ?>	

具体效果可以看看lyp.cniclass.net.cn

==================================================

有就显示没有就不显示,你觉得怎么样?
function catch_that_image() {
global $post, $posts;
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘//i’, $post->post_content, $matches);
$first_img = $matches

[0];
//这里干掉了默认图片。
return $first_img;
}

——————

<a href="”>
” title=”” />









http://lyp.cn/474_wordpress_auto_thumbnail?utm_source=tuicool&utm_medium=referral
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WordPress如何使文章列表样式多样化
bootstrap4 缩略图
WordPress 获取分类第一篇文章第一张图片地址
首页幻灯片中图片无法显示的解决办法
不用插件实现WordPress代码高亮显示
第十节,Bootstrap巨幕页头缩略图和警告框组件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服