admin管理员组文章数量:1122846
I have been struggling with this for hours now, any help appreciated. I am using Underscores theme with one sidebar (sidebar-1, on the left) and Custom Sidebars plugin so that I can have different side content on each page. The problem is when I add a new sidebar (sidebar-2) which I want to appear just above the footer to contain the same content on every page.
functions.php:
function myunderscores3_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'myunderscores3' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Add widgets here.', 'myunderscores3' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
//'before_title' => '<!--',
//'after_title' => '-->',
) );
register_sidebar( array(
'name' => esc_html__( 'Bottom Sidebar', 'myunderscores3' ),
'id' => 'sidebar-2',
'description' => esc_html__( 'Add widgets here.', 'myunderscores3' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
//'before_title' => '<!--',
//'after_title' => '-->',
) );
}
add_action( 'widgets_init', 'myunderscores3_widgets_init' );
sidebar.php (for the sidebar-1 content):
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
page.php (to pull in sidebar-1):
<?php
get_sidebar();
?>
</div><!-- wrapper -->
footer.php (to pull in sidebar-2):
if (is_active_sidebar( 'sidebar-2' )) {
dynamic_sidebar( 'sidebar-2' );
}
?>
</div><!-- #content -->
After adding widget content to sidebar-2 this is working fine on every page apart from the one page that has no sidebar-1 content. On this page the sidebar-2 content is appearing twice - on the bottom as expected and in sidebar-1 content area also ! Furthermore the 'no-sidebar' class is missing from the body. In the backend the sidebar-1 widgets are located through each individual page whereas the sidebar-2 content is located through the widget ('As Sidebar for selected Post Types' - Pages). On the actual page where the sidebar-2 content is appearing twice I have not specified any sidebars inside the actual page as I am doing this through the widget settings. Any ideas ? Thank you
I have been struggling with this for hours now, any help appreciated. I am using Underscores theme with one sidebar (sidebar-1, on the left) and Custom Sidebars plugin so that I can have different side content on each page. The problem is when I add a new sidebar (sidebar-2) which I want to appear just above the footer to contain the same content on every page.
functions.php:
function myunderscores3_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'myunderscores3' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Add widgets here.', 'myunderscores3' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
//'before_title' => '<!--',
//'after_title' => '-->',
) );
register_sidebar( array(
'name' => esc_html__( 'Bottom Sidebar', 'myunderscores3' ),
'id' => 'sidebar-2',
'description' => esc_html__( 'Add widgets here.', 'myunderscores3' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
//'before_title' => '<!--',
//'after_title' => '-->',
) );
}
add_action( 'widgets_init', 'myunderscores3_widgets_init' );
sidebar.php (for the sidebar-1 content):
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
page.php (to pull in sidebar-1):
<?php
get_sidebar();
?>
</div><!-- wrapper -->
footer.php (to pull in sidebar-2):
if (is_active_sidebar( 'sidebar-2' )) {
dynamic_sidebar( 'sidebar-2' );
}
?>
</div><!-- #content -->
After adding widget content to sidebar-2 this is working fine on every page apart from the one page that has no sidebar-1 content. On this page the sidebar-2 content is appearing twice - on the bottom as expected and in sidebar-1 content area also ! Furthermore the 'no-sidebar' class is missing from the body. In the backend the sidebar-1 widgets are located through each individual page whereas the sidebar-2 content is located through the widget ('As Sidebar for selected Post Types' - Pages). On the actual page where the sidebar-2 content is appearing twice I have not specified any sidebars inside the actual page as I am doing this through the widget settings. Any ideas ? Thank you
Share Improve this question asked Apr 18, 2019 at 9:02 rookieloopsrookieloops 12 Answers
Reset to default 0Template files are included, not called like a function. Even though you called get_sidebar()
, behind the scenes that is eventually calling include
. So in other words, your template pages (like footer.php, page.php, and sidebar.php) are not directly within a function, so you cannot 'return' out of them, as you are attempting to do in sidebar.php.
So WordPress is progressing past that point, and then running dynamic_sidebar( 'sidebar-1' );
. Now here's where someone else will have to jump in and tell you why it's loading the wrong widgets. I didn't think it would fallback to an active sidebar, but at least based on your code it seems to be. WordPress definitely does that with menus unless you specifically tell it not to.
But in any case, I think if you just modify your code you'll avoid the behavior entirely:
if ( is_active_sidebar( 'sidebar-1' ) ) :
?>
<aside id="secondary" class="widget-area">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
<?php endif; ?>
So all I did is use flip the check (removing the !) and then wrap the entire dynamic_sidebar section with it (using the alternate PHP format of : and endif;)
I have no idea what the problem was but after deleting all my code and creating the new side bar again it worked. maybe a typo somewhere or a caching issue ?
本文标签: Problem adding sidebar to Underscores theme
版权声明:本文标题:Problem adding sidebar to Underscores theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282928a1926800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论