【发布时间】:2017-01-02 04:56:54
【问题描述】:
我最近刚刚将博客网站上的所有内容导入 wordpress,我需要整理一下。
我在 single.php 中工作,我想从 the_content(); 获取每个 <a><img src=""/></a>。我的 php 充其量是有点粗制滥造。
我知道这可以让我获得帖子的第一张图片,但我需要类似的东西,可以让我获得来自 the_content(); 的所有图片(不是精选图片)。
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 = "/path/to/default.png";
}
return $first_img;
}
【问题讨论】: