2016-12-17 12 views
1

我创建了一个自定义主题和一个非常基本的布局我的WordPress(4.7)应用程序,它看起来是这样的:我怎样才能把多个侧边栏在我的模板的不同区域中的WordPress

enter image description here

在我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并把Metacontent-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> 
 
    ...

我做错了吗?

+0

评论不适用于扩展讨论;这次谈话一直[移动聊天](​​http://chat.stackoverflow.com/rooms/130830/discussion-on-question-by-subrata-sarkar-how-can-i-put-multiple-sidebars-in-差异)。 –

回答

1

问题 它包括once.that为什么它包括在同一个块中的所有侧边栏整个文件。

案例1:直接调用一个侧边栏(正常工作的)

<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的两个侧栏。现在我创建一个用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); ?> 
+0

@SubrataSarkar固定发送 –

+0

如果问题解决了,请标记为完整 –

+0

+1 :)我宁愿使用'Case-1',因为它更易于理解。我在不同的地方添加了三个侧边栏,他们的工作非常完美! “问题它包括整个文件一次。为什么它包括在同一个块中的所有边栏” - 这是最有价值的信息。非常感谢! –

相关问题