get_post_galleries_images()

最后更新于:2021-11-26 22:32:31

get_post_galleries_images( int|GC_Post$post)

Retrieve the image srcs from galleries from a post’s content, if present

参数

$post

(int|GC_Post) (Optional) Post ID or GC_Post object. Default is global $post.

响应

(array) A list of lists, each containing image srcs parsed. from an expanded shortcode

源文件

文件: gc-includes/media.php

function get_post_galleries_images( $post = 0 ) {
	$galleries = get_post_galleries( $post, false );
	return gc_list_pluck( $galleries, 'src' );
}
/**
 * Add list of image URLs to the content if displaying a post with one or more image galleries.
 *
 * @param string $content Post content.
 * @return string (Maybe modified) post content.
 */
function gcdocs_show_gallery_image_urls( $content ) {

	global $post;

	// Only do this on singular items.
	if ( ! is_singular() ) {
		return $content;
	}

	// Make sure the post has a gallery in it.
	if ( ! has_shortcode( $post->post_content, 'gallery' ) ) {
		return $content;
	}

	// Retrieve all galleries of this post.
	$galleries = get_post_galleries_images( $post );

	if ( ! empty( $galleries ) ) {
		$image_list = '<ul>';

		// Loop through all galleries found
		foreach( $galleries as $gallery ) {
			// Loop through each image in each gallery.
			foreach ( $gallery as $image ) {
				$image_list .= '<li>' . $image . '</li>';
			}
		}

		$image_list .= '</ul>';

		// Append our image list to the content of our post
		$content .= $image_list;
	}
	return $content;
 }
add_filter( 'the_content', 'gcdocs_show_gallery_image_urls' );