跳转到主要内容

URL 控件

Elementor URL 控件显示一个 URL 输入字段。该控件还能够显示更多控制选项,例如在新窗口中打开链接、添加 nofollow 属性以及定义额外属性(以逗号分隔的键值对列表)。

该控件在继承 Control_Base_Multiple 类的 Control_URL 类中定义。

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

参数

名称 类型 默认值 描述
type string url 控件的类型。
label string 显示在字段上方的标签。
description string 显示在字段下方的描述。
show_label bool true 是否显示标签。
label_block bool true 是否在单独一行显示标签。
separator string default 设置控件分隔符的位置。可用值为 default、before 和 after。default 将隐藏分隔符,除非控件类型有特定的分隔符设置。before / after 将分隔符定位在控件之前/之后。
placeholder string Paste URL or type 当字段没有值时显示的占位符。
autocomplete bool true 是否允许搜索功能。
options array|false [
'url',
'is_external',
'nofollow',
'custom_attributes'
]
要显示的 URL 选项数组。默认显示所有选项。但您可以选择显示哪些 URL 元素。将选项设置为 false 将禁用所有选项。
default array 字段的默认值。
  • $url (string) URL。
  • $is_external (bool) 是在同一标签页还是新标签页中打开 URL。
  • $nofollow (bool) 是否添加 nofollow 属性。
  • $custom_attributes (string) 自定义属性字符串,以逗号分隔的 key|value 对字符串形式提供。

返回值

[
	'url' => 'https://your-link.com',
	'is_external' => true,
	'nofollow' => true,
	'custom_attributes' => '',
]

(array) 包含链接数据的数组:

  • $url (string) URL 地址。
  • $is_external (bool) 是否在新标签页中打开链接。
  • $nofollow (bool) 是否添加 nofollow 属性。
  • $custom_attributes (string) 自定义属性字符串,格式为逗号分隔的 key|value 键值对。

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(
			'website_link',
			[
				'label' => esc_html__( 'Link', 'textdomain' ),
				'type' => \Elementor\Controls_Manager::URL,
				'options' => [ 'url', 'is_external', 'nofollow' ],
				'default' => [
					'url' => '',
					'is_external' => true,
					'nofollow' => true,
					// 'custom_attributes' => '',
				],
				'label_block' => true,
			]
		);

		$this->end_controls_section();

	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		if ( ! empty( $settings['website_link']['url'] ) ) {
			$this->add_link_attributes( 'website_link', $settings['website_link'] );
		}
		?>
		<a <?php $this->print_render_attribute_string( 'website_link' ); ?>>
			...
		</a>
		<?php
	}

	protected function content_template(): void {
		?>
		<a href="{{ settings.website_link.url }}">
			...
		</a>
		<?php
	}

}