我最近刚刚将博客网站上的所有内容导入到 WordPress 中,我需要稍微整理一下。
我正在 single.php 中工作,我想得到每个<a><img src=""/></a>
from the_content();
。我的 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;
}
看一下 PHP 的本机 DOMDocument 对象。http://www.php.net/manual/en/class.domdocument.php http://www.php.net/manual/en/class.domdocument.php
您可以通过以下方式获取您的内容并将其加载到 DOMDocument 中loadHTML()
。然后你可以使用getElementsByTagName()
获取所有图像。
http://www.php.net/manual/en/domdocument.getelementsbytagname.php http://www.php.net/manual/en/domdocument.getelementsbytagname.php
$document = new DOMDocument();
$document->loadHTML($post->post_content);
$images = $document->getElementsByTagName('img');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)