如何在wp post中获取所有外部图像

时间:2016-05-15 05:56:53

标签: php wordpress

我没有使用附件在wp post上传图片,
我使用像dropbox这样的外部主机,如何通过特定的post-type将它带到我的首页?

例如

<img src="https://www.dropbox.com/s/drgdrfhdtj.jpg?raw=1" alt="" width="506" height="673" />

<img src="https://www.dropbox.com/s/djtfdtjdtmjx.jpg?raw=1" alt="" width="506" height="714" />

如何获得所有这些图片?

我在function.php中的代码

function gallery (){ 

    $pid = get_the_ID();

$post = get_post( $pid );
$content = $post->post_content;
$regex = '/src="([^"]*)"/';
preg_match_all( $regex, $content, $matches );
foreach($matches as $image):

   echo '<img src="'.$image.'" alt="'.$post->post_title.'" title="'.$post->post_title.'">' ;

endforeach;
}

我的代码在content-gallery.php

<?php $post_format = get_post_format(); ?>

        <?php if ( $post_format == 'gallery' ) : ?>

            <div class="featured-media">    


                <?php gallery(); ?>
            </div> <!-- /featured-media -->


        <?php endif; ?>

但这不起作用

编辑:

work used image [1](@master djon answer)

现在问题是

输出:

<img src="https://www.dropbox.com/s/image1.jpg""><img https://www.dropbox.com/s/image1.jpg">    

双输出一张图片<img src=<img https://

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

$image是找到的匹配项中包含的所有组的数组。索引零是整个匹配(src="URL"),索引1是您搜索的内容:URL。

所以你应该$image[1]而不是$image