最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Widgets panel not displaying in the Theme Customizer

programmeradmin2浏览0评论

I am using the Theme Customizer to let users customize how their website looks.

I noticed on the Twenty Fifteen theme there is a panel for Widgets. I have seen this on quite a few other themes too, and the code for the panel has not been added to customizer.php (as far as I can tell)

On my theme, I have a few sidebars on the homepage. You can customize the widgets through Appearance > Widgets menus, however the Widgets panel in the customizer is not displaying.

How can I get it to show in the customizer so the user does not have to keep switching out to change the widgets?

My code for registering the sidebar:

function widgets_init_mysite() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'mytheme' ),
        'id' => 'sidebar-1',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
}

add_action( 'widgets_init', 'widgets_init_mysite' );

I add the sidebar to the page using dynamic_sidebar( 'sidebar-1' )

It is definitely displayed because I added widgets through Appearance > Widgets and I can see them in the customizer.

Note: One interesting thing I did find. I registered 5 Sidebars, with IDs of sidebar-1, sidebar-2 etc. In Firefix, I went to the theme customizer and Inspect Element. I found the Widgets panel existed, but had display: none. What is more interesting, in the ul sub-navigation, there were 5 li elements with the class section-sidebar-widgets-sidebar-1 (the last number changed for all the sidebars).

I checked the other sections I had made, and the class always started with section-, and then the section ID. I tried changing the panel of the sidebars to my panel like so:

$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'my-panel';

But nothing happened. This is weird because I know pretty much definitely know the names of the Sidebar Sections, but changing their panel does nothing...

I am using the Theme Customizer to let users customize how their website looks.

I noticed on the Twenty Fifteen theme there is a panel for Widgets. I have seen this on quite a few other themes too, and the code for the panel has not been added to customizer.php (as far as I can tell)

On my theme, I have a few sidebars on the homepage. You can customize the widgets through Appearance > Widgets menus, however the Widgets panel in the customizer is not displaying.

How can I get it to show in the customizer so the user does not have to keep switching out to change the widgets?

My code for registering the sidebar:

function widgets_init_mysite() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'mytheme' ),
        'id' => 'sidebar-1',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
}

add_action( 'widgets_init', 'widgets_init_mysite' );

I add the sidebar to the page using dynamic_sidebar( 'sidebar-1' )

It is definitely displayed because I added widgets through Appearance > Widgets and I can see them in the customizer.

Note: One interesting thing I did find. I registered 5 Sidebars, with IDs of sidebar-1, sidebar-2 etc. In Firefix, I went to the theme customizer and Inspect Element. I found the Widgets panel existed, but had display: none. What is more interesting, in the ul sub-navigation, there were 5 li elements with the class section-sidebar-widgets-sidebar-1 (the last number changed for all the sidebars).

I checked the other sections I had made, and the class always started with section-, and then the section ID. I tried changing the panel of the sidebars to my panel like so:

$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel = 'my-panel';

But nothing happened. This is weird because I know pretty much definitely know the names of the Sidebar Sections, but changing their panel does nothing...

Share Improve this question edited Dec 17, 2015 at 12:36 Kaspar Lee asked Dec 15, 2015 at 11:18 Kaspar LeeKaspar Lee 4074 silver badges19 bronze badges 13
  • You are asking about your code without showing it. How do you expect people to know what you are doing wrong? – Mark Kaplun Commented Dec 15, 2015 at 11:22
  • The only thing I can do is post all the code in the customizer.php file, and I am not comfortable doing that. I just want to know if there is some Wordpress feature that lets you enable that panel, not for my code to be debugged. – Kaspar Lee Commented Dec 15, 2015 at 12:48
  • No, the bug is in your code. – Mark Kaplun Commented Dec 15, 2015 at 13:15
  • No, I removed all the code from the customizer.php and the panel still did not show. – Kaspar Lee Commented Dec 15, 2015 at 13:27
  • Could you post the code you have for adding the widget areas? – Arcath Commented Dec 15, 2015 at 13:32
 |  Show 8 more comments

2 Answers 2

Reset to default 2

I see you're talking about the dynamic_sidebar function but I don't see you mention the sidebar file (eg. sidebar.php or sidebar-single.php).

There are basically 3 steps I follow to display sidebar widgets and they're always visible in customizer. If yours is not showing up, you probably may have missed something.

1. The register part in functions.php


$args = array(
    'name'          => __( 'Main Sidebar', 'mytheme' ),
    'id'            => 'sidebar-1',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget'  => '</div>',
    'before_title'  => '<h3 class="widget-title">',
    'after_title'   => '</h3>'
);
register_sidebar( $args );

2. The function call in a sidebar file (eg. sidebar.php or sidebar-single.php)


<?php
// Dynamic Sidebar
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-1') ) :
    // Sidebar fallback content
    get_sidebar();
endif;
// End Dynamic Sidebar Single posts
?>

3. Call the sidebar in the post/page template


<?php get_sidebar(); ?> or <?php get_sidebar('single'); ?> for sidebar-single.php

I would advise you to re-check your code to make sure you haven't left anything out. All the best!

Most likely you are not displaying the sidebar when the costumizer is active which prevents the costumizer from detecting its existence on the page.

The costumizer detects the sidebar by hooking on various sidebar related hooks that are supposed to be triggered when the page is generated. If for some reason your sidebar display code do not trigger the hooks the costumizer will not know it is there even if the actual content is being displayed. This can happen for example if you cache your sidebar and instead of calling dynamic_display you just output the sidebar.

发布评论

评论列表(0)

  1. 暂无评论