php – WordPress主题定制器 – 为用户添加区域以移动和组织小部件

前端之家收集整理的这篇文章主要介绍了php – WordPress主题定制器 – 为用户添加区域以移动和组织小部件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在开发一个wordpress主题,使用Theme Customizer让用户自定义它,但我已经卡住了.

对于页脚,我创建了各种小部件,包含不同的内容,如Recent Posts或Live Twitter Feed.

我希望用户能够按照他们想要的顺序组织它们,但我无法弄清楚如何做到这一点.我发现了另一个主题(Zerif Lite),它允许你这样做(见下图),但是我经历了所有的代码并且无法解决他们做到了,没有添加’我们的焦点部分小部件’部分.

我已经类似地组织了我的主题,有各种面板,有章节,我希望其中一个部分包含它.

编辑:

并非所有人都能解决我的问题.我知道如何创建小部件

我知道如何创建小部件.我希望主题定制器中的一个区域可以让用户移动它们,而不仅仅是我创建的区域,还有其他默认的区域,如Tag Cloud.

编辑2:@Codeartist,我使用的是wordpress 4.3.1,这是我在functions.PHP中的代码

  1. function widgets_init_mysite() {
  2. register_sidebar( array(
  3. '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>',) );
  4. }
  5.  
  6. add_action( 'widgets_init','widgets_init_mysite' );
  7.  
  8. function mytheme_customizer( $wp_customize ) {
  9.  
  10. $wp_customize->add_panel( 'panel_for_widgets',array(
  11. 'priority' => 70,'title' => __('Panel for widgets','codeartist'),'capability' => 'edit_theme_options',));
  12.  
  13. $wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'panel_for_widgets';
  14.  
  15. }
  16.  
  17. add_action( 'customize_register','mytheme_customizer' );
在查看zerif_customizer.js之后,我发现Zerif Lite正在通过JavaScript将widget面板部分添加到Theme Customizer中.

在Zerif Lite的儿童主题中做同样的事情……

在你的functions.PHP文件中:

  1. function mytheme_customizer_live_preview() {
  2. wp_enqueue_script(
  3. 'mytheme-themecustomizer',//Give the script an ID
  4. get_stylesheet_directory_uri() . '/js/theme-customizer.js',//Point to file
  5. array( 'jquery' ),//Define dependencies
  6. true //Put script in footer?
  7. );
  8. }
  9. add_action( 'customize_controls_enqueue_scripts','mytheme_customizer_live_preview' );

然后在主题中放入一个新的JavaScript文件,其中文件名和路径必须与上面函数中的第二个参数匹配:

  1. jQuery(document).ready(function() {
  2. /* Move our widgets into the widgets panel */
  3. wp.customize.bind('ready',function() {
  4. wp.customize.section( 'sidebar-widgets-sidebar-mysection' ).panel( 'panel_mysection' );
  5. wp.customize.section( 'sidebar-widgets-sidebar-mysection' ).priority( '2' );
  6. });
  7. });

当然,panel_mysection必须已经存在,如下所示:

  1. $wp_customize->add_panel( 'panel_mysection',array(
  2. 'priority' => 33,'capability' => 'edit_theme_options','title' => __( 'Mysection section','mytheme' )
  3. ));

sidebar-mysection小部件必须已经存在:

  1. class zerif_mysection extends WP_Widget {
  2. public function __construct() {
  3. parent::__construct(
  4. 'ctUp-ads-widget',__( 'Zerif - Mysection widget','zerif-lite' )
  5. );
  6. }
  7. }
  8. function mytheme_register_widgets() {
  9. register_widget('zerif_mysection');
  10. register_sidebar(
  11. array (
  12. 'name' => 'Mysection section widgets','id' => 'sidebar-mysection','before_widget' => '','after_widget' => ''
  13. )
  14. );
  15. }
  16. add_action('widgets_init','mytheme_register_widgets');

猜你在找的PHP相关文章