普通网友 2016-10-25 15:49
浏览 123
已采纳

在Wordpress主题中使用sidebar.php

I have recently installed Woocommerce on a self-built theme and got an error message telling me that I needed to add a sidebar.php code to my theme. The error message was followed by a list of widgets in a messy, un-styled list. The widgets included a search bar, a list of pages in my site and an archives and categories list. I have now created sidebar.php by following a tutorial which seems to have worked – I have included the code I used below.

What I'm finding unusual is that since adding the sidebar, these widgets seem to have disappeared and I now have to add them manually using the widgets tab under the appearance menu. I can't figure out why they might have disappeared?

I always assumed that when adding a sidebar, I'd have to add the widgets via PHP code – otherwise wouldn't the sidebar clash with other templates? For example, I can add the Woocommerce widgets for my shop pages, but when I want to create blog posts I'll need different widgets, and the Woocommerce ones will still be in the sidebar and vice-versa.

Maybe I've gone about doing this the wrong way.. perhaps somebody can point me in the right direction? I've been googling this and have watched some youtube tutorials but haven't found the answers I'm looking for.

My code is as follows:

functions.php

function enable_widgets() {

    register_sidebar(
        array(
            'name' => 'Main Sidebar',
            'id'   => 'sidebar',
            'description'   => 'Here you can add widgets to the main sidebar.',
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h5 id="widget-heading">',
            'after_title'   => '</h5>'
    ));

 }

 add_action('widgets_init','enable_widgets');

sidebar.php

<div class="sidebar">
    <?php dynamic_sidebar('sidebar'); ?>
</div>
  • 写回答

1条回答 默认 最新

  • dongzouche9108 2016-10-25 17:56
    关注

    In woocommerce/templates/global/sidebar.php, you can see how woocommerce displays the sidebar.

    <?php
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    get_sidebar( 'shop' ); ?>
    

    It will search for sidebar-shop.php first, and then sidebar.php. So all you need to do is create sidebar-shop.php under your theme folder.

    And in your sidebar-shop.php, display the widget area you have registered.

    sidebar-shop.php

    <div class="sidebar">
        <?php dynamic_sidebar('sidebar'); ?>
    </div>
    

    There is another approach to show woocommerce specific widget area using the same sidebar.php. Since WooCommerce has some conditional tags, we could use is_woocommerce() to determine the page uses WooCommerce templates or not.

    sidebar.php

    <div class="sidebar">
        <?php
    
        if ( is_woocommerce() ) {
           dynamic_sidebar('sidebar');
        } else {
           dynamic_sidebar('default-sidebar');
        }
    
       ?>
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值