Home  >  Article  >  Backend Development  >  Wordpress gets pictures from photo album

Wordpress gets pictures from photo album

WBOY
WBOYOriginal
2016-09-05 08:59:561706browse

If you insert only one Gallery containing multiple pictures into the article, is there any way to get the pictures in the album as thumbnails?

The two methods that come to mind now have proven to be ineffective. One is to use the Auto thumbnail plug-in to obtain featured images

<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>

After testing, it is invalid and the pictures in the album cannot be automatically set as thumbnails.

The second method is to get the first picture in the article and add it in function.php:

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

    preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $images);

    if($images){       
        return $images[1][0];
    }else{
        return false;
    }
}
</code>

Then use it wherever you need to call:

<code><img src="<?php echo get_content_first_image(get_the_content()); ?>"/>
</code>

After testing, the pictures in the album cannot be obtained.
The wordpress photo album is inserted via the shortcode [gallery id = "..."].
Is there any way to get the address of the image attachment in the album?

Reply content:

If you insert only one Gallery containing multiple pictures into the article, is there any way to get the pictures in the album as thumbnails?

The two methods that come to mind now have proven to be ineffective. One is to use the Auto thumbnail plug-in to obtain featured images

<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>

After testing, it is invalid and the pictures in the album cannot be automatically set as thumbnails.

The second method is to get the first picture in the article and add it in function.php:

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

    preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $images);

    if($images){       
        return $images[1][0];
    }else{
        return false;
    }
}
</code>

Then use it wherever you need to call:

<code><img src="<?php echo get_content_first_image(get_the_content()); ?>"/>
</code>

After testing, the pictures in the album cannot be obtained.
The wordpress photo album is inserted via the shortcode [gallery id = "..."].
Is there any way to get the address of the image attachment in the album?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn