Home > Backend Development > PHP Tutorial > Wordpress gets pictures from photo album

Wordpress gets pictures from photo album

WBOY
Release: 2016-09-05 08:59:56
Original
1727 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Then use it wherever you need to call:

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

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Then use it wherever you need to call:

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

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?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template