从wordpress帖子中的第一张图片中提取img src

时间:2010-08-28 19:57:42

标签: php wordpress extract image src

您好我正在尝试为wordpress网站创建2个函数:一个用于从帖子中的图像中提取src,另一个用于获取帖子slug。

目标是将一些代码放在如下所示的摘录中:

<a style="background: url( extracted img src using a function ) no-repeat scroll 0 0  transparent;" href=" post slug using a function ">

我打算为这段代码创建一个template.php文件,该文件将使用“wp include file”插件调用。这些函数将在我的主题文件夹中的functions.php文件中。

我发现了一个代码片段应该为图像src做技巧,但我不知道如何修改它来做我想做的事情。我遇到的主要问题是:

如何定位该帖子?

换句话说,如果这个模板包含在摘录中,那么函数需要定位属于该摘录的帖子。这意味着要包含在多个帖子摘录中。

如何获取post_slug?

我找到的片段在

之下

function get_img_src() {
   $src_content = $youroriginalhtmlwithimage;
   $firstImage = "";
   $output = preg_match_all('//i', $src_content, $ContentImages);
   $firstImage = $ContentImages[1] [0]; // To grab the first image
   echo $firstImage;
 }

1 个答案:

答案 0 :(得分:0)

哈哈,真好! ;)我曾经这样做过一次。 只需使用这个优秀的脚本:

Using regular expressions to extract the first image source from html codes?

相关问题