WordPress非正则获取文章第一张图片
网上流传的WordPress获取文章第一张图片作为缩略图的方法都是采用正则匹配抓取来实现的,代码如下: 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)){ $first_img = '/images/default.jpg'; } return $first_img; } 有人说这种方式效率太低,所以我写了第二种方法,首先通过get_children()函数来获取文章的第一张图片ID: …