当前位置:Gxlcms > PHP教程 > Wordpress获取相册中的图片

Wordpress获取相册中的图片

时间:2021-07-01 10:21:17 帮助过:4人阅读

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

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

  1. <code><!--?php
  2. $array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post--->ID), array(100,100));
  3. echo $array_image_url[0];
  4. ?></code>

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

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

  1. <code>function get_content_first_image($content){
  2. if ( $content === false ) $content = get_the_content();
  3. preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $images);
  4. if($images){
  5. return $images[1][0];
  6. }else{
  7. return false;
  8. }
  9. }
  10. </img.*?src=[\'"](.*?)[\'"].*?></code>

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

  1. <code><img src="https://www.gxlcms.com/<?php echo get_content_first_image(get_the_content()); ?>">
  2. </code>

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

回复内容:

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

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

  1. <code><!--?php
  2. $array_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post--->ID), array(100,100));
  3. echo $array_image_url[0];
  4. ?></code>

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

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

  1. <code>function get_content_first_image($content){
  2. if ( $content === false ) $content = get_the_content();
  3. preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $images);
  4. if($images){
  5. return $images[1][0];
  6. }else{
  7. return false;
  8. }
  9. }
  10. </img.*?src=[\'"](.*?)[\'"].*?></code>

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

  1. <code><img src="https://www.gxlcms.com/<?php echo get_content_first_image(get_the_content()); ?>">
  2. </code>

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

人气教程排行