# GET_POST_GALLERIES() │ WP 3.6.0
Функция get_post_galleries() извлекает галереи из содержания указанной записи (поста).
**Использует:**
- has_shortcode() — проверяет наличие шорткода в содержимом
- do_shortcode_tag() — выполняет шорткод
- get_shortcode_regex() — возвращает регулярное выражение для поиска шорткодов
**Вызывается функциями:**
- get_post_galleries_images()
- get_post_gallery()
**Производительность:**
- Время выполнения: 0.009364 сек (очень медленно) | 50000 раз: 538.27 сек (крайне медленно)
**Системные требования:**
- PHP 7.0.4
- WP 4.4.2
## Хуки от функции
- get_post_galleries
## Возвращает
Массив. Список массивов, каждый из которых содержит данные галереи и ссылки на изображения, извлеченные из шорткода.
## Применение
```php
get_post_galleries( $post, $html );
Параметры:
$post(int|WP_Post) (обязательный): ID записи или объект записи.$html(true|false): нужно ли вернуть HTML или данные в массиве. По умолчанию: true.
Примеры
Пример 1: Извлечение всех галерей из поста
Допустим, в тексте поста с ID 2179 содержится 2 галереи, представленные шорткодами :
$gal = get_post_galleries(2179, false);
/* $gal будет равен
Array
(
[0] => Array
(
[ids] => 6790,6789,6788
[src] => Array
(
[0] => http://wp-kama.ru/wp-content/uploads/2016/02/image12-80x80.png
[1] => http://wp-kama.ru/wp-content/uploads/2016/02/image11-80x80.png
[2] => http://wp-kama.ru/wp-content/uploads/2016/02/image10-80x80.png
)
)
[1] => Array
(
[ids] => 6762,6761,6760
[src] => Array
(
[0] => http://wp-kama.ru/wp-content/uploads/2016/02/image008-80x80.jpg
[1] => http://wp-kama.ru/wp-content/uploads/2016/02/image007-80x80.jpg
[2] => http://wp-kama.ru/wp-content/uploads/2016/02/image006-80x80.jpg
)
)
)
*/
Пример 2: С извлечением HTML разметки
$gal = get_post_galleries(25, true);
/* $gal будет равен
Array
(
[0] =>
[1] =>
)
*/
Изменения
Версия 3.6.0 — функци�� была введена.
Код функции
function get_post_galleries( $post, $html = true ) {
$post = get_post( $post );
if ( ! $post ) {
return array();
}
if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) {
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 ( ! isset( $shortcode_attrs['id'] ) ) {
$shortcode[3] .= ' id="' . (int) $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 ) ),
)
);
}
}
}
}
if ( has_block( 'gallery', $post->post_content ) ) {
$post_blocks = parse_blocks( $post->post_content );
while ( $block = array_shift( $post_blocks ) ) {
$has_inner_blocks = ! empty( $block['innerBlocks'] );
if ( ! $block['blockName'] ) {
continue;
}
if ( 'core/gallery' !== $block['blockName'] ) {
if ( $has_inner_blocks ) {
array_push( $post_blocks, ...$block['innerBlocks'] );
}
continue;
}
if ( $has_inner_blocks && $html ) {
$block_html = wp_list_pluck( $block['innerBlocks'], 'innerHTML' );
$galleries[] = '' . implode( ' ', $block_html ) . ' ';
continue;
}
$srcs = array();
if ( $has_inner_blocks ) {
$attrs = wp_list_pluck( $block['innerBlocks'], 'attrs' );
$ids = wp_list_pluck( $attrs, 'id' );
foreach ( $ids as $id ) {
$url = wp_get_attachment_url( $id );
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
$srcs[] = $url;
}
}
$galleries[] = array(
'ids' => implode( ',', $ids ),
'src' => $srcs,
);
continue;
}
if ( $html ) {
$galleries[] = $block['innerHTML'];
continue;
}
$ids = ! empty( $block['attrs']['ids'] ) ? $block['attrs']['ids'] : array();
if ( ! empty( $ids ) ) {
foreach ( $ids as $id ) {
$url = wp_get_attachment_url( $id );
if ( is_string( $url ) && ! in_array( $url, $srcs, true ) ) {
$srcs[] = $url;
}
}
$galleries[] = array(
'ids' => implode( ',', $ids ),
'src' => $srcs,
);
continue;
}
preg_match_all( '#src=(['"])(.+?)1#is', $block['innerHTML'], $found_srcs, PREG_SET_ORDER );
if ( ! empty( $found_srcs[0] ) ) {
foreach ( $found_srcs as $src ) {
if ( isset( $src[2] ) && ! in_array( $src[2], $srcs, true ) ) {
$srcs[] = $src[2];
}
}
}
$galleries[] = array( 'src' => $srcs );
}
}
return apply_filters( 'get_post_galleries', $galleries, $post );
}
Связанные функции
gallery_shortcode()get_post_galleries_images()get_post_gallery()get_post_gallery_images()





