forked from loorlab/get-post-gallery-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-post-gallery-images.php
49 lines (47 loc) · 1.99 KB
/
get-post-gallery-images.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
Plugin Name: Get Post Gallery Images
Description: Used to fetch images from your posts galleries to display as part of the excerpt.
Tags: future, upcoming posts, upcoming post, upcoming, draft, Post, scheduled, preview
Author: Sierj Khaletski
Version: 0.1
*/
/**
* Get Image from Post core file
*
* This file contains all the logic required for the plugin
*
* @package Get Post Gallery Images
* @copyright Copyright (c) 2013, Sierj Khaletski
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, v2 (or newer)
*
* @since Get Post Gallery Images 0.1
*/
function get_post_gallery_images_gpgi() {
$attachment_ids = array();
$pattern = get_shortcode_regex();
$images = array();
if (preg_match_all( '/'. $pattern .'/s', get_the_content(), $matches ) ) {
//finds the "gallery" shortcode and puts the image ids in an associative array at $matches[3]
$count = count($matches[3]); //in case there is more than one gallery in the post.
for ($i = 0; $i < $count; $i++){
$atts = shortcode_parse_atts( $matches[3][$i] );
if ( isset( $atts['ids'] ) ){
$attachment_ids = explode( ',', $atts['ids'] );
$attachementsCount = count($attachment_ids);
if ($attachementsCount > 0){
for ($j = 0; $j < $attachementsCount ; $j++) {
$image = array();
$attachmentId = intval($attachment_ids[$j]);
$image['id'] = $attachmentId;
$image['full'] = wp_get_attachment_image_src($attachmentId, 'full');
$image['medium'] = wp_get_attachment_image_src($attachmentId, 'medium');
$image['thumbnail'] = wp_get_attachment_image_src($attachmentId, 'thumbnail');
array_push($images, $image);
}
}
}
}
}
return $images;
}