返回介绍

get_post_galleries()

发布于 2017-09-10 23:52:57 字数 3206 浏览 959 评论 0 收藏 0

get_post_galleries( int|WP_Post $post,  bool $html = true )

Retrieves galleries from the passed post’s content.


description


参数

$post

(int|WP_Post) (Required) Post ID or object.

$html

(bool) (Optional) Whether to return HTML or data in the array.

Default value: true


返回值

(array) A list of arrays, each containing gallery data and srcs parsed from the expanded shortcode.


源代码

File: wp-includes/media.php

function get_post_galleries( $post, $html = true ) {
	if ( ! $post = get_post( $post ) )
		return array();

	if ( ! has_shortcode( $post->post_content, 'gallery' ) )
		return array();

	$galleries = array();
	if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
		foreach ( $matches as $shortcode ) {
			if ( 'gallery' === $shortcode[2] ) {
				$srcs = array();

				$shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
				if ( ! is_array( $shortcode_attrs ) ) {
					$shortcode_attrs = array();
				}

				// Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already.
				if ( ! isset( $shortcode_attrs['id'] ) ) {
					$shortcode[3] .= ' id="' . intval( $post->ID ) . '"';
				}

				$gallery = do_shortcode_tag( $shortcode );
				if ( $html ) {
					$galleries[] = $gallery;
				} else {
					preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
					if ( ! empty( $src ) ) {
foreach ( $src as $s ) {
$srcs[] = $s[2];
}
					}

					$galleries[] = array_merge(
$shortcode_attrs,
array(
'src' => array_values( array_unique( $srcs ) )
)
					);
				}
			}
		}
	}

	/**
	 * Filters the list of all found galleries in the given post.
	 *
	 * @since 3.6.0
	 *
	 * @param array   $galleries Associative array of all found post galleries.
	 * @param WP_Post $post      Post object.
	 */
	return apply_filters( 'get_post_galleries', $galleries, $post );
}

更新日志

Versiondescription
3.6.0Introduced.

相关函数

Uses

  • wp-includes/shortcodes.php: has_shortcode()
  • wp-includes/shortcodes.php: get_shortcode_regex()
  • wp-includes/shortcodes.php: shortcode_parse_atts()
  • wp-includes/shortcodes.php: do_shortcode_tag()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: get_post_galleries
  • wp-includes/post.php: get_post()
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/media.php: get_post_gallery()
  • wp-includes/media.php: get_post_galleries_images()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文