web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. seo教程
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站

wordpress如何自动获取文章中第一张图片为缩略图

455 ℃
     

wordpress如何自动获取文章中第一张图片为缩略图,下面web建站小编给大家简单介绍一下具体代码!

1、打开当前主题functions.php文件,新增以下代码:

function wp_content_image() {
    global $post;
 $content = $post->post_content;
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
    $n = count($strResult[1]);
    if($n > 0){
    echo '<dt><a href="'.get_permalink().'"><img src="'.$strResult[1][0].'"  alt="'.trim(strip_tags( $post->post_title )).'"/></a></dt>';
    }
}

2、页面中调用代码:

<?php echo wp_content_image(); ?>

php如何只获取字符串中的英文字母

wordpress如何直接调用特色图片(has_post_thumbnail)

wordpress有图片显示图片没图片不显示

wordpress栏目添加缩略图封面代码介绍

织梦cms相关文章likearticle显示缩略图

标签: preg_match, preg_match_all, 缩略图, 自动获取

上面是“wordpress如何自动获取文章中第一张图片为缩略图”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3340.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > wordpress
本文共计485个字,预计阅读时长4分钟
生活小工具,收录了80多款小工具
上一篇: WordPress用自定义代码实现更高级的分页功能
下一篇: 车牌号输入HTML模板下载
x 打工人ai神器