get_post_gallery()

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

get_post_gallery( int|GC_Post$post, bool$html=true)

Check a specified post’s content for gallery and, if present, return the first

参数

$post

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

$html

(bool) (Optional) Whether to return HTML or data. Default is true.

Default value: true

响应

(string|array) Gallery data and srcs parsed from the expanded shortcode.

源文件

文件: gc-includes/media.php

function get_post_gallery( $post = 0, $html = true ) {
	$galleries = get_post_galleries( $post, $html );
	$gallery   = reset( $galleries );

	/**
	 * Filters the first-found post gallery.
	 *
	 * @since 3.6.0
	 *
	 * @param array       $gallery   The first-found post gallery.
	 * @param int|GC_Post $post      Post ID or object.
	 * @param array       $galleries Associative array of all found post galleries.
	 */
	return apply_filters( 'get_post_gallery', $gallery, $post, $galleries );
}
<?php
/* The loop */
while ( have_posts() ) : the_post();
	if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
		// Loop through all the image and output them one by one.
		foreach ( $gallery['src'] AS $src ) {
	                ?>                
	                <img src="https://docs.gechiui.com/functions/get_post_gallery/<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
			<?php
		}
	endif;
endwhile;
?>