0

我有<?php the_content(); ?>生成html标签的功能:

<p>
 <a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg">
  <img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
 </a>
</p> 

我试图提取<img.**./>但我不成功。

类似于:

//$String =
<p>
 <a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg">
  <img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
 </a>
</p> 

$String = "the_content()";

$pattern = '/^<img/.*/$/>/';//Take string wich start with "<img" and all between and End in "/>"
preg_match($pattern, substr($subject,3), $StrImg, PREG_OFFSET_CAPTURE);
echo $StrImg[0];
output
//<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />

如何设置变量$StrImg完整的 img 标签?

非常感谢。

4

1 回答 1

3

因此,您想获取发布中所有图像的 src 属性。有更简单的方法:

$images = get_posts(array(
            'post_parent' => $post->ID,
            'post_type' => 'attachment',
            'numberposts' => -1,
            'post_mime_type' => 'image'
        ))

        foreach( $images as $image ) {
            $image_url[] = wp_get_attachment_image_src( $image->ID, full );

        }
于 2012-09-30T13:16:13.163 回答