wordpress如何获取文章图片链接?

2020-10-09 12:05发布

想提取wordpress文章的缩略图,WordPress获取文章中第一张图片地址怎么弄?


1条回答
花紫南
1楼 · 2020-10-09 14:24.采纳回答

可以通过catch that image 函数来实现,具体操作步骤如下:

1、在functions.php中加入代码:

<?php
function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/< class>"]([^'"]+)['"].*>/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;
    }
?>

2、在要调用图片的地方加入代码:

"<?php echo catch_that_image() ?>" />