The post_gallery WordPress PHP filter allows you to modify the default gallery shortcode output. If the filtered output isn’t empty, it will be used instead of generating the default gallery template.
Usage
add_filter('post_gallery', 'your_custom_function', 10, 3); function your_custom_function($output, $attr, $instance) < // Your custom code here return $output; >
Parameters
$output (string) – The gallery output. Default empty.
$attr (array) – Attributes of the gallery shortcode.
$instance (int) – Unique numeric ID of this gallery shortcode instance.
More information
See WordPress Developer Resources: post_gallery
Examples
Customize the Gallery HTML Structure
Change the default gallery output by creating a custom HTML structure.
add_filter('post_gallery', 'custom_gallery_html', 10, 3); function custom_gallery_html($output, $attr, $instance) < // Retrieve the images from the gallery $ids = explode(',', $attr['ids']); $output = '
Получает первую галерею из текста указанной записи/поста. Ищет шорткод в тексте, обрабатывает его и возвращает массив данных картинок галереи. Аналог функции get_post_galleries(), только получает данные первой галереи, а не всех имеющихся.
Хуки из функции
Возвращает
массив — Список картинок галереи.
строку HTML — Если параметр $html=true то вернет строку — HTML код галереи.
false — Если на странице галерея отсутствует.
Использование
get_post_gallery( $post, $html );
$post(число/WP_Post) (обязательный) ID или объект записи в тексте которой нужно найти первую галерею. $html(логический) В данных возвращать готовый HTML галереи или данные галереи в виде ID вложений и ссылок на картинки этих вложений. По умолчанию: true
Image galleries are the best way to showcase your pictures on your WordPress sites. WordPress bundles the Create Gallery feature by default in the media uploader which allows you to create a simple gallery.
Note: Before adding a gallery, you must have images in your media library. Otherwise, you need to upload the images into the library and can proceed on gallery creation.
Gallery shortcode
The Gallery feature allows you to add one or more image galleries to your posts and pages using a simple Shortcode.
The basic form of gallery shortcode is:
Tip: If you use the shortcode without using the ids argument in your post or page, only images that are “attached” to that post or page will be displayed.
If you need to add multiple images with ID’s, use the following sample shortcode
//Note: 10, 205, 552 and 607 are the IDs of respected image.
Tip: NOTE: find the proper IDs of the images for the gallery. Go to Media library and click on the respected image and ID will appear on the URL.
To use the shortcode from the template file, use the do_shortcode() function. Insert the following code into your template file:
If you need to use the shortcode with IDs, insert the following code in your template file:
Usage
There are may options that may be specified using the below syntax:
If you want to print the gallery directly on the template file, use `do_shortcode() ` function like below:
If you need to filter the shortcodes, the following example gives you some tips
// Note: 'the_content' filter is used to filter the content of the // post after it is retrieved from the database and before it is // printed to the screen.
Supported Options
Gallery Shortcodes supports the basic options which are listed below:
Orderby
‘orderby’ specifies the order the thumbnails show up. The default order is ‘menu_order’.
menu_order: You can reorder the images in the Gallery tab of the Add Media popup
title: Order by the title of the image in the Media Library
post_date: Sort by date/time
rand: Order randomly
ID: Specify the post ID
Order
order specify the sort order used to display thumbnail; ASC or DESC. For Example, to sort by ID and DESC:
If you need to print it on template file, use the do_shortcode() function;
columns
The Columns options specify the number of columns in the gallery. The default value is 3. If you want to increase the number of column in the galley, use the following shortcode.
If you need to print it on your template file, use the do_shortcode() function;
IDs
The IDs option on the gallery shortcode loads images with specific post IDs.
If you want to display the attached image with the specific post ID, follow the following code example.
// Note: remove each space between brackets and 'gallery' and brackets and `123"`. //Here "123" stands for the post IDs. If you want to display more than //one ID, separate the IDs by a comma `,`. [ gallery ]
Use ‘do_shortcode’ function to print the gallery with IDs on template files like below:
// Note: remove each space between brackets and 'gallery' and brackets and `123"`.
Size
For example, to display a gallery of medium sized images:
Some advanced options are also available on Gallery shortcodes.
itemtag
The name of the HTML tag used to enclose each item in the gallery. The default is “dl”.
icontag
The name of the HTMLtag used to enclose each thumbnail icon in the gallery. The default is “dt”.
captiontag
The name of the HTML tag used to enclose each caption. The default is “dd”.
You are allowed to change the defaults.
Link
Specify where you want the image to link. The default value links to the attachment’s permalink. Options:
Include
Include allows you to insert an “array” of comma separated attachment IDs to show only the images from these attachments.
Exclude
Exclude callows you to insert an “array” of comma separated attachment IDs to not show the images from these attachments. Please note that include and exclude cannot be used together.
References
For more technical details take a reference from below links
Массив . Список картинок галереи или нескольких галерей. Если параметр $html=true то массив будет содержать готовый HTML код каждой галереи.
Использование
get_post_galleries( $post, $html );
$post(число/WP_Post) (обязательный) ID или объект записи в тексте которой нужно найти галереи. $html(логический) В данных возвращать готовый HTML галереи или данные галереи в виде ID вложений и ссылок на картинки. По умолчанию: true
Примеры
#1 Получим все галере записи 2179
Этот пример показывает как работает функция. Предполагается что в тексте записи 2179 присутствуют 2 галереи — 2 шорткода :