WordPress使用get_attached_media函数获取图片附件

网站APP 10 个月前 回复

, ,

PHP代码:

<?php
   $images = get_attached_media('image' ); // get attached media  
    foreach ($images as $image)  {
    $ximage =  wp_get_attachment_image_src($image->ID,'large');
    echo '<div><img src="' .$ximage[0] . '" class="img-responsive" /></div>';
                          }
?>

输出的结果:

<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-1-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-2-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-3-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-4-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-5-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-6-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-7-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-8-1024x682.jpg" class="img-responsive" /></div>
<div><img src="https://example.com/wp-content/uploads/2018/12/Image_Title-9-1024x682.jpg" class="img-responsive" /></div>

原文:https://www.javaroad.cn/questions/3222
参考官方文档:https://developer.wordpress.org/reference/functions/get_attached_media/

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)