欢迎来到HELLO素材网! 南京网站制作选择顺炫科技
丰富的DIV CSS模版、JS,jQuery特效免费提供下载
当前位置:主页 > 建站教程 > CMS教程 >

WordPress技巧:主动显示文章第一张图片

发表于2019-04-18 10:34| 次阅读| 来源网络整理| 作者session

摘要:WordPress技巧:主动显示文章第一张图片

WordPress技巧:主动显示文章第一张图片

在修正主题的时分,咱们能够需求WordPress主动获取文章第一张图片,在博客吧看到了相干的完成方法,记载一下,预计当前也用失去。
  
  在以后利用的主题模板的functions.php文件<?php和?>之前减少以下代码
  
    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] [0];
        if(empty($first_img)){ //Defines a default image
          $first_img = "/images/default.jpg";
        }
        return $first_img;
      }
 

  在以后主题模板的index.php文件的内容代码前或后减少以下代码
  
 <?php echo catch_that_image() ?>  
  
      当然了,要限度显示图片的效果(比如大小、边框),就需求你利用css设置了。