阿里云服务器

我们使用的wordpress博客主题一般都是缩略图加文字的形式显示,一般的主题缩略图就是发布的时候设置的特色图片。

下面介绍如果让wordpress网站自动抓取文章中第一张图作为缩略图的方法。

第一步:在主题的functions.php函数中加入下面代码:

//实现获取文章第一张图
function wp_that_image() {
global $post, $posts;
ob_start();
ob_end_clean();
preg_match('/<imgs[^<>]*?src=['"]([^'"<>]+?)['"][^<>]*?>/i', $post->post_content, $matche);
if($matche[1])
return $matche[1];
//如果没有图,我们可以自定义一张
return 'img.jpg';
}

第二步:在模板中进行调用,代码如下:

<img src="<?php echo wp_that_image(); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="240px" height="180px" />

图片大小可以根据自己页面的需要进行调整。

相关阅读:
  • wordpress编辑框不让span过滤的方法
  • wordpress搜索为空时仍然返回结果问题
  • wordpress注册页取消邮箱的方法
  • wordpress用户头像在线调用代码
  • wordpress自动隐藏email注册,直接用户名密码注册
  • wordpress设置游客只能访问指定页面的方法
  • wordpress列表分页显示404页面的解决办法
  • wordpress页面默认排序修改的方法
  • wordpress新版本5.4 “Adderley” 发布上线,更新多项功能
  • wordpress默认编辑器功能增加的方法