最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress自动调用文章缩略图的方法总结
时间:2014-03-30 编辑:简简单单 来源:一聚教程网
一、自动显示文章第一张图片
在当前使用的主题模板的functions.php文件之前添加以下代码
代码如下 | 复制代码 |
function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/ $first_img = $matches [1] [0]; if(empty($first_img)){ //Defines a default image $first_img = "/images/default.jpg"; } return $first_img; } |
在当前主题模板的index.php文件的内容代码前或后添加以下代码
代码如下 | 复制代码 |
二、文章列表页自动调用文章缩略图
在外观–编辑里头找到functions.php,加入以下这个函数:
代码如下 | 复制代码 |
function emtx_auto_thumbnail($pID,$thumb='thumbnail') { $blogimg = FALSE; if (has_post_thumbnail()) {// 判断该文章是否已经设置了“特色图像”,如果有则直接显示该特色图像的缩略图 www.111com.net $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('/]*src=["|']([^"|']+)/i', get_the_content(), $match) != FALSE) { $blogimg = $match[1]; } if($blogimg) { $blogimg = ''; } return $blogimg; } |
然后在相应的模板文件里面调用缩略图的地方做个修改,把原来调用the_post_thumbnail的地方按照实际需求改为诸如下面这样的代码即可:
代码如下 | 复制代码 |
ID) ) { |
三、后台所有文章列表显示缩略图
打开你主题的functions.php文件添加如下代码:
代码如下 | 复制代码 |
if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) { |
如后台的效果
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31