get_post_gallery_images()

最后更新于:2021-11-26 22:33:12

get_post_gallery_images( int|GC_Post$post)

Checks a post’s content for galleries and return the image srcs for the first found gallery

参数

$post

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

响应

(string[]) A list of a gallery’s image srcs in order.

源文件

文件: gc-includes/media.php

function get_post_gallery_images( $post = 0 ) {
	$gallery = get_post_gallery( $post, false );
	return empty( $gallery['src'] ) ? array() : $gallery['src'];
}
 add_filter( 'the_content', 'gcdocs_show_gallery_image_urls' );

/**
 * Show image URLs below the 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 the first gallery in the post
	$gallery = get_post_gallery_images( $post );
	$image_list = '<ul>';

	// Loop through each image in each gallery
	foreach( $gallery as $image_url ) {
		$image_list .= '<li>' . '<img src="' . $image_url . '">' . '</li>';
	}
	$image_list .= '</ul>';

	// Append our image list to the content of our post
	$content .= $image_list;

	return $content;
 }