函数文档

get_post_galleries_images()

💡 云策文档标注

概述

get_post_galleries_images() 函数用于从文章内容中提取图库的图片 src 地址,如果存在的话。它基于 get_post_galleries() 函数,返回一个包含图片 src 列表的数组。

关键要点

  • 函数 get_post_galleries_images( $post = 0 ) 接受一个可选参数 $post,可以是文章 ID 或 WP_Post 对象,默认使用全局 $post。
  • 返回值是一个数组,每个元素是一个列表,包含从扩展的短代码中解析出的图片 src 地址。
  • 内部实现依赖于 get_post_galleries() 和 wp_list_pluck() 函数来提取 src 字段。
  • 该函数自 WordPress 3.6.0 版本引入。

代码示例

/**
 * 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 wpdocs_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 = '';

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

        $image_list .= '';

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

📄 原文内容

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

Description

See also

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.

Return

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

Source

function get_post_galleries_images( $post = 0 ) {
	$galleries = get_post_galleries( $post, false );
	return wp_list_pluck( $galleries, 'src' );
}

Changelog

Version Description
3.6.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    A simple example of how to append the raw image URLs to the content of any post or page that has at least one gallery.

    /**
     * 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 wpdocs_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', 'wpdocs_show_gallery_image_urls' );