Wordpress 获取相册中的图片

WBOY
发布: 2016-09-05 08:59:56
原创
1666 人浏览过

如果在文章中只插入一个Gallery包含多张图片,有没有什么办法能获得相册中的图片作为缩略图?

现在想到的两个方法都证明无效了,一个是配合Auto thumbnail插件然后获取特色图片

<code><?php $array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(100,100));
echo $array_image_url[0];
?></code>
登录后复制
登录后复制

经过测试是无效的,无法将相册里的图片自动设置为缩略图。

第二个方法是获取文章中第一个图片,通过function.php中增加:

<code>function get_content_first_image($content){
    if ( $content === false ) $content = get_the_content(); 

    preg_match_all('|<img . alt="Wordpress 获取相册中的图片" >|i', $content, $images);

    if($images){       
        return $images[1][0];
    }else{
        return false;
    }
}
</img.></code>
登录后复制
登录后复制

然后在需要调用的地方使用:

<code><img  src="<?php%20echo%20get_content_first_image(get_the_content());%20?>" alt="Wordpress 获取相册中的图片" >
</code>
登录后复制
登录后复制

经过测试也无法获取相册中的图片。
wordpress相册是通过短代码[gallery id = "..."]插入的。
有什么办法能获取相册中图片附件的地址吗?

回复内容:

如果在文章中只插入一个Gallery包含多张图片,有没有什么办法能获得相册中的图片作为缩略图?

现在想到的两个方法都证明无效了,一个是配合Auto thumbnail插件然后获取特色图片

<code><?php $array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array(100,100));
echo $array_image_url[0];
?></code>
登录后复制
登录后复制

经过测试是无效的,无法将相册里的图片自动设置为缩略图。

第二个方法是获取文章中第一个图片,通过function.php中增加:

<code>function get_content_first_image($content){
    if ( $content === false ) $content = get_the_content(); 

    preg_match_all('|<img . alt="Wordpress 获取相册中的图片" >|i', $content, $images);

    if($images){       
        return $images[1][0];
    }else{
        return false;
    }
}
</img.></code>
登录后复制
登录后复制

然后在需要调用的地方使用:

<code><img  src="<?php%20echo%20get_content_first_image(get_the_content());%20?>" alt="Wordpress 获取相册中的图片" >
</code>
登录后复制
登录后复制

经过测试也无法获取相册中的图片。
wordpress相册是通过短代码[gallery id = "..."]插入的。
有什么办法能获取相册中图片附件的地址吗?

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!