我没有在wp-post中使用附件上传图像,
我使用像dropbox这样的外部主机,如何通过特定的帖子类型将其放到我的首页?
示例
<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" />
如何获取所有这些图像?
函数中的我的代码.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-galley.php 中的代码
<?php $post_format = get_post_format(); ?>
<?php if ( $post_format == 'gallery' ) : ?>
<div class="featured-media">
<?php gallery(); ?>
</div> <!-- /featured-media -->
<?php endif; ?>
但是不起作用
编辑:
工作使用的图像[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://
如何解决这个问题?
$image
是找到的匹配中包含的所有组的数组。索引0是整个匹配(src="URL"
),索引1是您搜索的内容:URL。
所以你应该用$image[1]
而不是$image
。