【发布时间】:2012-04-03 18:11:09
【问题描述】:
在一个 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)){ //Defines a default image
$first_img = "http://custome_url_for_default_image.png";
}
return $first_img;
}
我尝试将它按原样粘贴到 Tumblr 主题中,但遇到了一些问题(它不会作为 PHP 函数加载)。我肯定错过了一些东西。如果有人有解决此问题的想法,我很乐意尝试。
谢谢,
P.
【问题讨论】:
-
是的,抱歉:网络抓取、抓取、收获……我还意识到我需要 PHP 脚本的开始和结束标记,但添加它们也不能使函数正常工作。
标签: php image wordpress tumblr