跳转到主要内容

视觉选择控件

Elementor 视觉选择控件将单选按钮显示为带图像的按钮组。

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

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

参数

名称 类型 默认值 描述
type string visual_choice 控件的类型。
label string 显示在字段上方的标签。
description string 显示在字段下方的描述。
show_label bool true 是否显示标签。
label_block bool true 是否在单独一行显示标签。
separator string default 设置控件分隔符的位置。可用值为 default、before 和 after。default 将隐藏分隔符,除非控件类型有特定的分隔符设置。before / after 会将分隔符定位在控件之前/之后。
options array [] 一个多维数组,包含每个单选按钮的 title 和 image。
columns number 1 网格布局中的列数。
toggle bool true 是否允许切换/取消选择。
default string 字段的默认值。

Return Value

(string) The selected option value.

Usage

<?php
class Elementor_Test_Widget extends \Elementor\Widget_Base {

	protected function register_controls(): void {

		$this->start_controls_section(
			'style_section',
			[
				'label' => esc_html__( 'Style', 'textdomain' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'structure',
			[
				'label' => esc_html__( 'Structure', 'textdomain' ),
				'type' => \Elementor\Controls_Manager::VISUAL_CHOICE,
				'label_block' => true,
				'options' => [
					'grid-3' => [
						'title' => esc_attr__( 'Grid: 3 columns.', 'textdomain' ),
						'image' => plugins_url( 'assets/img/grid-3.svg', __FILE__ ),
					],
					'masonry' => [
						'title' => esc_attr__( 'Masonry: arbitrary order', 'textdomain' ),
						'image' => plugins_url( 'assets/img/masonry.svg', __FILE__ ),
					],
					'stacked' => [
						'title' => esc_attr__( 'Stacked: Single column.', 'textdomain' ),
						'image' => plugins_url( 'assets/img/stacked.svg', __FILE__ ),
					],
					'focus' => [
						'title' => esc_attr__( 'Focus: Text only.', 'textdomain' ),
						'image' => plugins_url( 'assets/img/focus.svg', __FILE__ ),
					],
					'grid-2' => [
						'title' => esc_attr__( 'Grid: 2 columns.', 'textdomain' ),
						'image' => plugins_url( 'assets/img/grid2.svg', __FILE__ ),
					],
					'stretch' => [
						'title' => esc_attr__( 'Stretch: fit available width.', 'textdomain' ),
						'image' => plugins_url( 'assets/img/stretch.svg', __FILE__ ),
					]
				],
				'default' => 'masonry',
				'columns' => 2,
				'prefix_class' => 'some-layout-',
			]
		);

		$this->end_controls_section();

	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		?>
		<div class="your-class">
			...
		</div>
		<?php
	}

	protected function content_template(): void {
		?>
		<div class="your-class">
			...
		</div>
		<?php
	}

}