类文档

WP_Customize_Background_Position_Control

💡 云策文档标注

概述

WP_Customize_Background_Position_Control 是 WordPress 自定义器中的一个控件类,用于设置背景位置。它继承自 WP_Customize_Control,通过 JavaScript 渲染内容,提供直观的界面选择背景位置选项。

关键要点

  • 继承自 WP_Customize_Control,类型为 'background_position'。
  • 使用 JavaScript 模板渲染控件内容,PHP 中不直接渲染。
  • 提供九个标准背景位置选项(如左上、居中、右下),每个选项带有标签和图标。
  • 从 WordPress 4.7.0 版本开始引入。

代码示例

public function content_template() {
    $options = array(
        array(
            'left top'   => array(
                'label' => __( 'Top Left' ),
                'icon'  => 'dashicons dashicons-arrow-left-alt',
            ),
            'center top' => array(
                'label' => __( 'Top' ),
                'icon'  => 'dashicons dashicons-arrow-up-alt',
            ),
            'right top'  => array(
                'label' => __( 'Top Right' ),
                'icon'  => 'dashicons dashicons-arrow-right-alt',
            ),
        ),
        array(
            'left center'   => array(
                'label' => __( 'Left' ),
                'icon'  => 'dashicons dashicons-arrow-left-alt',
            ),
            'center center' => array(
                'label' => __( 'Center' ),
                'icon'  => 'background-position-center-icon',
            ),
            'right center'  => array(
                'label' => __( 'Right' ),
                'icon'  => 'dashicons dashicons-arrow-right-alt',
            ),
        ),
        array(
            'left bottom'   => array(
                'label' => __( 'Bottom Left' ),
                'icon'  => 'dashicons dashicons-arrow-left-alt',
            ),
            'center bottom' => array(
                'label' => __( 'Bottom' ),
                'icon'  => 'dashicons dashicons-arrow-down-alt',
            ),
            'right bottom'  => array(
                'label' => __( 'Bottom Right' ),
                'icon'  => 'dashicons dashicons-arrow-right-alt',
            ),
        ),
    );
    ?>
    
        {{{ data.label }}}
    
    
        {{{ data.description }}}
    
    
        
            
                
                    
                
                
                
                    
                     $input ) : ?>
                        
                            ">
                            " aria-hidden="true">
                            
                        
                    
                    
                
                
            
        
    View all references View on Trac View on GitHub
    
    Related UsesDescriptionWP_Customize_Controlwp-includes/class-wp-customize-control.php  
    Changelog VersionDescription4.7.0Introduced.

📄 原文内容

Customize Background Position Control class.

Description

See also

Methods

Name Description
WP_Customize_Background_Position_Control::content_template Render a JS template for the content of the position control.
WP_Customize_Background_Position_Control::render_content Don’t render the control content from PHP, as it’s rendered via JS on load.

Source

class WP_Customize_Background_Position_Control extends WP_Customize_Control {

/**
* Type.
*
* @since 4.7.0
* @var string
*/
public $type = 'background_position';

/**
* Don't render the control content from PHP, as it's rendered via JS on load.
*
* @since 4.7.0
*/
public function render_content() {}

/**
* Render a JS template for the content of the position control.
*
* @since 4.7.0
*/
public function content_template() {
$options = array(
array(
'left top' => array(
'label' => __( 'Top Left' ),
'icon' => 'dashicons dashicons-arrow-left-alt',
),
'center top' => array(
'label' => __( 'Top' ),
'icon' => 'dashicons dashicons-arrow-up-alt',
),
'right top' => array(
'label' => __( 'Top Right' ),
'icon' => 'dashicons dashicons-arrow-right-alt',
),
),
array(
'left center' => array(
'label' => __( 'Left' ),
'icon' => 'dashicons dashicons-arrow-left-alt',
),
'center center' => array(
'label' => __( 'Center' ),
'icon' => 'background-position-center-icon',
),
'right center' => array(
'label' => __( 'Right' ),
'icon' => 'dashicons dashicons-arrow-right-alt',
),
),
array(
'left bottom' => array(
'label' => __( 'Bottom Left' ),
'icon' => 'dashicons dashicons-arrow-left-alt',
),
'center bottom' => array(
'label' => __( 'Bottom' ),
'icon' => 'dashicons dashicons-arrow-down-alt',
),
'right bottom' => array(
'label' => __( 'Bottom Right' ),
'icon' => 'dashicons dashicons-arrow-right-alt',
),
),
);
?>
<# if ( data.label ) { #>
<span class="customize-control-title">{{{ data.label }}}</span>
<# } #>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{{ data.description }}}</span>
<# } #>
<div class="customize-control-content">
<fieldset>
<legend class="screen-reader-text"><span>

</span></legend>
<div class="background-position-control">

<div class="button-group">
$input ) : ?>
<label>
<input class="ui-helper-hidden-accessible" name="background-position" type="radio" value="<?php echo esc_attr( $value ); ?>">
<span class="button display-options position"><span class="<?php echo esc_attr( $input['icon'] ); ?>" aria-hidden="true"></span></span>
<span class="screen-reader-text"></span>
</label>

</div>

</div>
</fieldset>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/customize/class-wp-customize-background-position-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-background-position-control.php#L17">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/customize/class-wp-customize-background-position-control.php#L17-L116">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.7.0 Introduced.