跳转到主要内容

画廊控件

Elementor 画廊控件基于 WordPress 画廊显示一个媒体选择区域。该控件允许从 WordPress 媒体库中选择多张图片。

该控件在继承 Base_Data_Control 类的 Control_Gallery 类中定义。

使用此控件时,应将 type 设置为 \Elementor\Controls_Manager::GALLERY 常量。

参数

名称 类型 默认值 描述
type string gallery 控件的类型。
label string 显示在字段上方的标签。
description string 显示在字段下方的描述。
show_label bool true 是否显示标签。
label_block bool true 是否在单独一行显示标签。
separator string default 设置控件分隔符的位置。可用值为 default、before 和 after。default 将隐藏分隔符,除非控件类型有特定的分隔符设置。before / after 会将分隔符定位在控件之前/之后。
default array 默认图库图片。包含图片 ID 和 URL 的图片数组。

返回值

[
	[
		'id' => 0,
		'url' => ''
	],
	[
		'id' => 0,
		'url' => ''
	],
]

(array) 一个由单个图片数组组成的数组,包含每个图片的 ID 和 URL:

  • $id (int) 媒体 ID。
  • $url (string) 媒体 URL。

Usage

<?php
class Elementor_Test_Widget extends \Elementor\Widget_Base {

	protected function register_controls(): void {

		$this->start_controls_section(
			'content_section',
			[
				'label' => esc_html__( 'Content', 'textdomain' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'gallery',
			[
				'label' => esc_html__( 'Add Images', 'textdomain' ),
				'type' => \Elementor\Controls_Manager::GALLERY,
				'show_label' => false,
				'default' => [],
			]
		);

		$this->end_controls_section();

	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		foreach ( $settings['gallery'] as $image ) {
			echo '<img src="' . esc_attr( $image['url'] ) . '">';
		}
	}

	protected function content_template(): void {
		?>
		<# _.each( settings.gallery, function( image ) { #>
			<img src="{{ image.url }}">
		<# }); #>
		<?php
	}

}