WP_Customize_Nav_Menu_Location_Control
概述
WP_Customize_Nav_Menu_Location_Control 是 WordPress 自定义器中的一个控件类,专门用于处理导航菜单位置的选择。它继承自 WP_Customize_Control,主要用于 JavaScript 交互。
关键要点
- 这是一个自定义控件类,仅用于 JavaScript 端,继承自 WP_Customize_Control。
- 控件类型为 'nav_menu_location',包含 location_id 属性用于标识菜单位置。
- 主要方法包括 to_json() 用于传递参数到 JavaScript,以及 render_content() 用于渲染类似普通选择控件的内容。
- 在 render_content() 中,会根据是否有值显示或隐藏相关元素,并支持创建菜单的按钮(自 4.9.0 版本起)。
代码示例
class WP_Customize_Nav_Menu_Location_Control extends WP_Customize_Control {
public $type = 'nav_menu_location';
public $location_id = '';
public function to_json() {
parent::to_json();
$this->json['locationId'] = $this->location_id;
}
public function render_content() {
if ( empty( $this->choices ) ) {
return;
}
$value_hidden_class = '';
$no_value_hidden_class = '';
if ( $this->value() ) {
$value_hidden_class = ' hidden';
} else {
$no_value_hidden_class = ' hidden';
}
?>
label ) ) : ?>
label ); ?>
description ) ) : ?>
description; ?>
link(); ?>>
choices as $value => $label ) :
echo 'value(), $value, false ) . '>' . esc_html( $label ) . '';
endforeach;
?>
" data-location-id="location_id ); ?>" aria-label="">
" aria-label="">
View all references View on Trac View on GitHub
Related UsesDescriptionWP_Customize_Controlwp-includes/class-wp-customize-control.php
Changelog VersionDescription4.3.0Introduced. Customize Menu Location Control Class.
Description
This custom control is only needed for JS.
See also
Methods
| Name | Description |
|---|---|
| WP_Customize_Nav_Menu_Location_Control::render_content | Render content just like a normal select control. |
| WP_Customize_Nav_Menu_Location_Control::to_json | Refresh the parameters passed to JavaScript via JSON. |
Source
class WP_Customize_Nav_Menu_Location_Control extends WP_Customize_Control {/**
* Control type.
*
* @since 4.3.0
* @var string
*/
public $type = 'nav_menu_location';/**
* Location ID.
*
* @since 4.3.0
* @var string
*/
public $location_id = '';/**
* Refresh the parameters passed to JavaScript via JSON.
*
* @since 4.3.0
*
* @see WP_Customize_Control::to_json()
*/
public function to_json() {
parent::to_json();
$this->json['locationId'] = $this->location_id;
}/**
* Render content just like a normal select control.
*
* @since 4.3.0
* @since 4.9.0 Added a button to create menus.
*/
public function render_content() {
if ( empty( $this->choices ) ) {
return;
}$value_hidden_class = '';
$no_value_hidden_class = '';
if ( $this->value() ) {
$value_hidden_class = ' hidden';
} else {
$no_value_hidden_class = ' hidden';
}
?>
<label>
label ) ) : ?>
<span class="customize-control-title">label ); ?></span>description ) ) : ?>
<span class="description customize-control-description">description; ?></span><select <?php $this->link(); ?>>
choices as $value => $label ) :
echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . esc_html( $label ) . '</option>';
endforeach;
?>
</select>
</label>
<button type="button" class="button-link create-menu<?php echo $value_hidden_class; ?>" data-location-id="<?php echo esc_attr( $this->location_id ); ?>" aria-label="<?php esc_attr_e( 'Create a menu for this location' ); ?>"></button>
<button type="button" class="button-link edit-menu<?php echo $no_value_hidden_class; ?>" aria-label="<?php esc_attr_e( 'Edit selected menu' ); ?>"></button>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/customize/class-wp-customize-nav-menu-location-control.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/customize/class-wp-customize-nav-menu-location-control.php#L19">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/customize/class-wp-customize-nav-menu-location-control.php#L19-L89">View on GitHub</a></p></section><section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/classes/wp_customize_control/">WP_Customize_Control</a><code>wp-includes/class-wp-customize-control.php
Changelog
| Version | Description |
|---|---|
| 4.3.0 | Introduced. |