wordpress日志缩略图功能实现
来源:互联网 发布:北京 纹眉 知乎 编辑:程序博客网 时间:2024/05/28 14:57
首先给大家展示一下,目前设置缩略图的四种方法(详见下图),我按照自己的理解将其排列成如下顺序,并且在图中列出了各种实现缩略图的方法的图片来源,通过自定义字段可以调用外链图片,而后面三种方法仅能使用本地图片。
下面我通过分层条件判断语句的方式,将上述四种缩略图调用方式整合到一起,并设置成四个顺序执行的环节,只有当上面的条件不满足的时候,才会顺次对下面的条件进行判断,并执行相应的语句。(详见下图)
下面就到了见证奇迹的时刻,请看缩略图实现代码。
<div class="thumb"> <?php if ( get_post_meta($post->ID, '缩略图', true) ){ ?> <?php $image = get_post_meta($post->ID, '缩略图', true); ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > <img width="160px" height="160px" src="<?php echo $image; ?>" alt="<?php the_title(); ?>"/></a> <?php } elseif(has_post_thumbnail()){?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" ><img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=160&w=160&zc=1" alt=""/></a> <?php } elseif(catch_that_image()){?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > <img src="<?php echo catch_that_image()?>" width="160px" height="160px" alt="<?php the_title(); ?>"/></a> <br /> <?php } else { ?> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > <img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php bloginfo('template_directory'); ?>/images/thumbnail.jpg&h=160&w=160&zc=1" alt="" /> </a> <?php } ?> </div>
为了使上述代码能够流畅的运行,还有几件事情需要做;
第一件事情是确保当前所用wordpress主题支持特色图像功能,查看一下主题的function文件中是否有
add_theme_support('post-thumbnails')
这一语句,没有的话请添加上;
第二件事情是为了实现抓取正文中第一张图片做缩略图的功能,需要在当前主题的function文件中添加如下函数:
function catch_that_image(){
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] ;
if(empty($first_img)){ //Defines a default image
$first_img ="0";
}
return $first_img;
};
- wordpress日志缩略图功能实现
- wordpress文章缩略图功能
- [wordpress技巧]屏蔽WordPress日志修订功能
- Wordpress 无插件实现自动显示缩略图
- 实现wordpress路由功能
- yii2 缩略图功能的实现
- wordpress怎样实现投稿功能?
- WordPress插件实现图片缩略图幻灯展示效果
- wordpress插件multiple-post-thumbnails实现多缩略图教程
- dedecms实现图集的缩略图功能
- js实现缩略图功能的源代码
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
- OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能
- 修改wordpress缩略图尺寸
- Wordpress 首页文章缩略图
- 为wordpress开启缩略图
- wordpress日志中的全部图片以缩略图的形式显示在文章摘要底部
- WordPress中如何实现Ping功能
- 关于navicat连接oracle10g 报 ORA-12737 set CHS16GBK错误的解决方案
- PHP提高编程效率的方法
- hibernate与tomcat和weblogic配置数据源的不同之处
- 数据结构HASH总结五:Nginx中的HASH(version 0.1)
- 取出html代码中的一段文字 去除html标签
- wordpress日志缩略图功能实现
- qcow2、raw、vmdk等镜像格式
- uart(一)
- C++类模板 实现顺序栈
- Android中APK安装过程及原理解析
- 简单C# 遍历文件和文件夹
- Nginx的phase初始化
- IP数据包分析与解析
- 淘宝面试题 放鸡蛋问题