Wordpress Theme Customizer - 添加区域供用户四处移动和组织小部件
Wordpress Theme Customizer - Add area for users to move around and organize widgets
我目前正在开发一个 Wordpress 主题,使用 Theme Customizer 让用户自定义它,但是我卡住了。
对于页脚,我创建了各种小部件,其中包含不同的内容,例如最近的帖子或实时 Twitter 提要。
我希望用户能够按照他们想要的顺序组织它们,但我不知道该怎么做。我找到了另一个主题 (Zerif Lite),它可以让您执行此操作(见下图),但是我检查了所有代码,但无法确定他们做到了,没有添加 'Our focus section widgets' 部分。
我以类似的方式组织了我的主题,有各种面板和部分,我希望其中一个部分包含它。
编辑:
似乎并不是每个人都能理解我的问题。 我知道如何创建小部件
我知道如何创建小部件。我想要主题定制器中的一个区域供用户移动它们,不仅是我创建的那些,还有其他默认的,比如标签云。
编辑 2: @Codeartist,我使用的是 Wordpress 4.3.1,这是我在 functions.php
中的代码
function widgets_init_mysite() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'widgets_init_mysite' );
function mytheme_customizer( $wp_customize ) {
$wp_customize->add_panel( 'panel_for_widgets', array(
'priority' => 70,
'title' => __('Panel for widgets', 'codeartist'),
'capability' => 'edit_theme_options',
));
$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'panel_for_widgets';
}
add_action( 'customize_register', 'mytheme_customizer' );
我正在试验最新更新的二十一主题。
在 function.php 中注册了一些侧边栏:
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Showcase Sidebar', 'twentyeleven' ),
'id' => 'sidebar-2',
'description' => __( 'The sidebar for the optional Showcase Template', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area One', 'twentyeleven' ),
'id' => 'sidebar-3',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area Two', 'twentyeleven' ),
'id' => 'sidebar-4',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area Three', 'twentyeleven' ),
'id' => 'sidebar-5',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
每个侧边栏都有自己唯一的 ID。如果您的主题中启用了小部件和侧边栏,那么默认的 'widgets' 面板将由 Wordpress 在定制器屏幕上创建。然后为每个侧边栏创建放置在 'widgets' 面板中的部分。该部分具有基于侧边栏 ID 的 ID。这个 id 看起来像这样
sidebar-widgets-[sidebar-id]
其中 sidebar-id 是相应侧边栏的 id。
你所有的代码都应该放在functions.php(或插件内部)的'customize_register' hook
add_action( 'customize_register', 'codeartist_customize_register' );
function codeartist_customize_register($wp_customize) {
//Put your code here
}
所以,基本上,我们需要做的就是创建新面板
$wp_customize->add_panel( 'panel_for_widgets', array(
'priority' => 70,
'title' => __('Panel for widgets', 'codeartist'),
'capability' => 'edit_theme_options',
));
然后在该面板中移动代表边栏的所有部分。
$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-2' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-3' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-4' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-5' )->panel = 'panel_for_widgets';
二十一有五个边栏,所以我们移动了五个部分。
最后,每个部分的名称与各自侧边栏的名称相同。要更改该部分的描述,您可以这样做。
$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->description= __('New description', 'codeartist');
遗憾的是,关于 get_section 的文档不多,但这里是 link 的抄本:https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/get_section
您要使用的是 WordPress 中的主题定制器,它有一组独特的挂钩和围绕它的 API。
开始使用这个钩子,customize_register 钩子:
https://developer.wordpress.org/reference/hooks/customize_register/
围绕主题定制器构建了一个相当强大的API,您可以在使用它时参考本手册以获取文档:
https://developer.wordpress.org/themes/advanced-topics/customizer-api/
深入了解 zerif_customizer.js
后,我发现 Zerif Lite 正在通过 JavaScript 将小部件面板部分添加到主题定制器中。
要在 Zerif Lite 的子主题中做同样的事情...
在您的 functions.php
文件中:
function mytheme_customizer_live_preview() {
wp_enqueue_script(
'mytheme-themecustomizer', //Give the script an ID
get_stylesheet_directory_uri() . '/js/theme-customizer.js', //Point to file
array( 'jquery' ), //Define dependencies
true //Put script in footer?
);
}
add_action( 'customize_controls_enqueue_scripts', 'mytheme_customizer_live_preview' );
然后在您的主题中放入一个新的 JavaScript 文件,文件名和路径必须与上述函数的第二个参数相匹配:
jQuery(document).ready(function() {
/* Move our widgets into the widgets panel */
wp.customize.bind('ready', function() {
wp.customize.section( 'sidebar-widgets-sidebar-mysection' ).panel( 'panel_mysection' );
wp.customize.section( 'sidebar-widgets-sidebar-mysection' ).priority( '2' );
});
});
当然,panel_mysection
必须已经存在,如下所示:
$wp_customize->add_panel( 'panel_mysection', array(
'priority' => 33,
'capability' => 'edit_theme_options',
'title' => __( 'Mysection section', 'mytheme' )
));
并且 sidebar-mysection
小部件部分必须已经存在:
class zerif_mysection extends WP_Widget {
public function __construct() {
parent::__construct(
'ctUp-ads-widget',
__( 'Zerif - Mysection widget', 'zerif-lite' )
);
}
}
function mytheme_register_widgets() {
register_widget('zerif_mysection');
register_sidebar(
array (
'name' => 'Mysection section widgets',
'id' => 'sidebar-mysection',
'before_widget' => '',
'after_widget' => ''
)
);
}
add_action('widgets_init', 'mytheme_register_widgets');
我目前正在开发一个 Wordpress 主题,使用 Theme Customizer 让用户自定义它,但是我卡住了。
对于页脚,我创建了各种小部件,其中包含不同的内容,例如最近的帖子或实时 Twitter 提要。
我希望用户能够按照他们想要的顺序组织它们,但我不知道该怎么做。我找到了另一个主题 (Zerif Lite),它可以让您执行此操作(见下图),但是我检查了所有代码,但无法确定他们做到了,没有添加 'Our focus section widgets' 部分。
我以类似的方式组织了我的主题,有各种面板和部分,我希望其中一个部分包含它。
编辑:
似乎并不是每个人都能理解我的问题。 我知道如何创建小部件
我知道如何创建小部件。我想要主题定制器中的一个区域供用户移动它们,不仅是我创建的那些,还有其他默认的,比如标签云。
编辑 2: @Codeartist,我使用的是 Wordpress 4.3.1,这是我在 functions.php
function widgets_init_mysite() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'widgets_init_mysite' );
function mytheme_customizer( $wp_customize ) {
$wp_customize->add_panel( 'panel_for_widgets', array(
'priority' => 70,
'title' => __('Panel for widgets', 'codeartist'),
'capability' => 'edit_theme_options',
));
$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'panel_for_widgets';
}
add_action( 'customize_register', 'mytheme_customizer' );
我正在试验最新更新的二十一主题。
在 function.php 中注册了一些侧边栏:
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Showcase Sidebar', 'twentyeleven' ),
'id' => 'sidebar-2',
'description' => __( 'The sidebar for the optional Showcase Template', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area One', 'twentyeleven' ),
'id' => 'sidebar-3',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area Two', 'twentyeleven' ),
'id' => 'sidebar-4',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Footer Area Three', 'twentyeleven' ),
'id' => 'sidebar-5',
'description' => __( 'An optional widget area for your site footer', 'twentyeleven' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
每个侧边栏都有自己唯一的 ID。如果您的主题中启用了小部件和侧边栏,那么默认的 'widgets' 面板将由 Wordpress 在定制器屏幕上创建。然后为每个侧边栏创建放置在 'widgets' 面板中的部分。该部分具有基于侧边栏 ID 的 ID。这个 id 看起来像这样
sidebar-widgets-[sidebar-id]
其中 sidebar-id 是相应侧边栏的 id。
你所有的代码都应该放在functions.php(或插件内部)的'customize_register' hook
add_action( 'customize_register', 'codeartist_customize_register' );
function codeartist_customize_register($wp_customize) {
//Put your code here
}
所以,基本上,我们需要做的就是创建新面板
$wp_customize->add_panel( 'panel_for_widgets', array(
'priority' => 70,
'title' => __('Panel for widgets', 'codeartist'),
'capability' => 'edit_theme_options',
));
然后在该面板中移动代表边栏的所有部分。
$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-2' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-3' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-4' )->panel = 'panel_for_widgets';
$wp_customize->get_section( 'sidebar-widgets-sidebar-5' )->panel = 'panel_for_widgets';
二十一有五个边栏,所以我们移动了五个部分。
最后,每个部分的名称与各自侧边栏的名称相同。要更改该部分的描述,您可以这样做。
$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->description= __('New description', 'codeartist');
遗憾的是,关于 get_section 的文档不多,但这里是 link 的抄本:https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/get_section
您要使用的是 WordPress 中的主题定制器,它有一组独特的挂钩和围绕它的 API。
开始使用这个钩子,customize_register 钩子:
https://developer.wordpress.org/reference/hooks/customize_register/
围绕主题定制器构建了一个相当强大的API,您可以在使用它时参考本手册以获取文档:
https://developer.wordpress.org/themes/advanced-topics/customizer-api/
深入了解 zerif_customizer.js
后,我发现 Zerif Lite 正在通过 JavaScript 将小部件面板部分添加到主题定制器中。
要在 Zerif Lite 的子主题中做同样的事情...
在您的 functions.php
文件中:
function mytheme_customizer_live_preview() {
wp_enqueue_script(
'mytheme-themecustomizer', //Give the script an ID
get_stylesheet_directory_uri() . '/js/theme-customizer.js', //Point to file
array( 'jquery' ), //Define dependencies
true //Put script in footer?
);
}
add_action( 'customize_controls_enqueue_scripts', 'mytheme_customizer_live_preview' );
然后在您的主题中放入一个新的 JavaScript 文件,文件名和路径必须与上述函数的第二个参数相匹配:
jQuery(document).ready(function() {
/* Move our widgets into the widgets panel */
wp.customize.bind('ready', function() {
wp.customize.section( 'sidebar-widgets-sidebar-mysection' ).panel( 'panel_mysection' );
wp.customize.section( 'sidebar-widgets-sidebar-mysection' ).priority( '2' );
});
});
当然,panel_mysection
必须已经存在,如下所示:
$wp_customize->add_panel( 'panel_mysection', array(
'priority' => 33,
'capability' => 'edit_theme_options',
'title' => __( 'Mysection section', 'mytheme' )
));
并且 sidebar-mysection
小部件部分必须已经存在:
class zerif_mysection extends WP_Widget {
public function __construct() {
parent::__construct(
'ctUp-ads-widget',
__( 'Zerif - Mysection widget', 'zerif-lite' )
);
}
}
function mytheme_register_widgets() {
register_widget('zerif_mysection');
register_sidebar(
array (
'name' => 'Mysection section widgets',
'id' => 'sidebar-mysection',
'before_widget' => '',
'after_widget' => ''
)
);
}
add_action('widgets_init', 'mytheme_register_widgets');