如何在 WordPress 模板的不同区域放置多个侧边栏
How can I put multiple sidebars in different areas of my template in WordPress
我为我的 WordPress (4.7) 应用程序创建了一个自定义主题和一个非常基本的布局,如下所示:
在我的 functions.php
文件中我注册了一些 sidebars
.
function flowershouse_widgets_init(){
register_sidebar(array(
'name' => __('Main content left', 'flowershouse'),
'id' => 'content-main-left',
'description' => __('Add widgets to appear in left column of main content area', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Main content right', 'flowershouse'),
'id' => 'content-main-right',
'description' => __('Add widgets to appear in right column of main content area', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Sidebar', 'flowershouse'),
'id' => 'sidebar-1',
'description' => __('Add widgets here to appear in sidebar', 'flowershouse'),
'before_widget' => '<section id="%1$s" class="%2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Footer Left', 'flowershouse'),
'id' => 'footer-left',
'description' => __('Add widgets to appear in left footer', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Footer Middle', 'flowershouse'),
'id' => 'footer-middle',
'description' => __('Add widgets here to appear in middle footer column', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Footer Right', 'flowershouse'),
'id' => 'footer_right',
'description' => __('Add widgets here to appear in Right footer column', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_widget' => '<h2>'
));
}
add_action('widgets_init', 'flowershouse_widgets_init');
所有已注册的侧边栏都可以在 Appearance > Widgets
页面下找到。在 sidebar.php
文件中我有这个:
if (is_active_sidebar( 'sidebar-1' )) {
dynamic_sidebar( 'sidebar-1' );
}
if (is_active_sidebar('content-main-left')){
dynamic_sidebar('content-main-left');
}
我将搜索小部件放在 sidebar-1
下,将 Meta
放在 content-main-left
侧边栏下。但是当页面呈现时,两个小部件都显示在同一个侧边栏下。这就是我的 index.php
的样子:
...
<div class="row margin-t20">
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="box box-gray padd-10-all">
Left column with 9 column
<div class="row margin-t10">
<div class="col-md-3 col-sm-3 col-xs-12">
<div class="box box-white padd-10-all">
<!-- Here I want 'content-main-left' sidebar widgets to render -->
<?php get_sidebar('content-main-left'); ?>
</div>
</div>
<div class="col-md-9 col-sm-9 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
Right content<br />(inside 9-column left column)
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
<!-- Here I want 'sidebar-1' sidebar widgets to render -->
<?php get_sidebar('sidebar-1'); ?>
</div>
</div>
<div class="clearfix"></div>
</div>
...
我做错了什么?
问题
它在 once.that 包含整个文件,为什么它在同一块中包含所有侧边栏。
案例一:直接调用侧边栏(正常工作)
<div class="row margin-t20">
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="box box-gray padd-10-all">
Left column with 9 column
<div class="row margin-t10">
<div class="col-md-3 col-sm-3 col-xs-12">
<div class="box box-white padd-10-all">
<?php if (is_active_sidebar('content-main-left')){dynamic_sidebar('content-main-left');} ?>
</div>
</div>
<div class="col-md-9 col-sm-9 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
Right content<br />(inside 9-column left column)
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- Right Side bar -->
<div class="col-md-3 col-sm-3 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
<?php if (is_active_sidebar('fw_sidebar')){dynamic_sidebar('fw_sidebar');} ?>
</div>
</div>
<div class="clearfix"></div>
</div>
案例 2:创建文件并调用
创建两个文件并添加sidebar.i.e的代码。我有一个名为 sidebar-1 和 sidebar-2.Now 的两个侧边栏,我为此创建了一个名为 slug 的文件。
文件名:边栏-1.php
if (is_active_sidebar( 'fw_sidebar' )) {
dynamic_sidebar( 'fw_sidebar' );
}
文件名:边栏-2.php
if (is_active_sidebar('content-main-left')){
dynamic_sidebar('content-main-left');
}
调用此侧边栏时只需调用函数 get_sidebar(slugname)。例如,
<?php get_sidebar(1); ?>
<?php get_sidebar(2); ?>
我为我的 WordPress (4.7) 应用程序创建了一个自定义主题和一个非常基本的布局,如下所示:
在我的 functions.php
文件中我注册了一些 sidebars
.
function flowershouse_widgets_init(){
register_sidebar(array(
'name' => __('Main content left', 'flowershouse'),
'id' => 'content-main-left',
'description' => __('Add widgets to appear in left column of main content area', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Main content right', 'flowershouse'),
'id' => 'content-main-right',
'description' => __('Add widgets to appear in right column of main content area', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Sidebar', 'flowershouse'),
'id' => 'sidebar-1',
'description' => __('Add widgets here to appear in sidebar', 'flowershouse'),
'before_widget' => '<section id="%1$s" class="%2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Footer Left', 'flowershouse'),
'id' => 'footer-left',
'description' => __('Add widgets to appear in left footer', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Footer Middle', 'flowershouse'),
'id' => 'footer-middle',
'description' => __('Add widgets here to appear in middle footer column', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
register_sidebar(array(
'name' => __('Footer Right', 'flowershouse'),
'id' => 'footer_right',
'description' => __('Add widgets here to appear in Right footer column', 'flowershouse'),
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_widget' => '<h2>'
));
}
add_action('widgets_init', 'flowershouse_widgets_init');
所有已注册的侧边栏都可以在 Appearance > Widgets
页面下找到。在 sidebar.php
文件中我有这个:
if (is_active_sidebar( 'sidebar-1' )) {
dynamic_sidebar( 'sidebar-1' );
}
if (is_active_sidebar('content-main-left')){
dynamic_sidebar('content-main-left');
}
我将搜索小部件放在 sidebar-1
下,将 Meta
放在 content-main-left
侧边栏下。但是当页面呈现时,两个小部件都显示在同一个侧边栏下。这就是我的 index.php
的样子:
...
<div class="row margin-t20">
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="box box-gray padd-10-all">
Left column with 9 column
<div class="row margin-t10">
<div class="col-md-3 col-sm-3 col-xs-12">
<div class="box box-white padd-10-all">
<!-- Here I want 'content-main-left' sidebar widgets to render -->
<?php get_sidebar('content-main-left'); ?>
</div>
</div>
<div class="col-md-9 col-sm-9 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
Right content<br />(inside 9-column left column)
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="col-md-3 col-sm-3 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
<!-- Here I want 'sidebar-1' sidebar widgets to render -->
<?php get_sidebar('sidebar-1'); ?>
</div>
</div>
<div class="clearfix"></div>
</div>
...
我做错了什么?
问题 它在 once.that 包含整个文件,为什么它在同一块中包含所有侧边栏。
案例一:直接调用侧边栏(正常工作)
<div class="row margin-t20">
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="box box-gray padd-10-all">
Left column with 9 column
<div class="row margin-t10">
<div class="col-md-3 col-sm-3 col-xs-12">
<div class="box box-white padd-10-all">
<?php if (is_active_sidebar('content-main-left')){dynamic_sidebar('content-main-left');} ?>
</div>
</div>
<div class="col-md-9 col-sm-9 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
Right content<br />(inside 9-column left column)
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- Right Side bar -->
<div class="col-md-3 col-sm-3 col-xs-12 mobile-v-space">
<div class="box box-white padd-10-all">
<?php if (is_active_sidebar('fw_sidebar')){dynamic_sidebar('fw_sidebar');} ?>
</div>
</div>
<div class="clearfix"></div>
</div>
案例 2:创建文件并调用
创建两个文件并添加sidebar.i.e的代码。我有一个名为 sidebar-1 和 sidebar-2.Now 的两个侧边栏,我为此创建了一个名为 slug 的文件。
文件名:边栏-1.php
if (is_active_sidebar( 'fw_sidebar' )) {
dynamic_sidebar( 'fw_sidebar' );
}
文件名:边栏-2.php
if (is_active_sidebar('content-main-left')){
dynamic_sidebar('content-main-left');
}
调用此侧边栏时只需调用函数 get_sidebar(slugname)。例如,
<?php get_sidebar(1); ?>
<?php get_sidebar(2); ?>