Register_sidebar()可以在wordpress后端工作,但不能在前端工作


register_sidebar() works in wordpress backend but not in frontend

我正在尝试实现自定义侧边栏到我的自定义wordpress模板。

我把这个添加到我的functions.php:

if ( function_exists('register_sidebar'))
register_sidebar(array(
    'name'          =>  'NiceBar',
    'id'            =>  'sidebar-widget',
    'before_widget' =>  '<li>',
    'after_widget'  =>  '</li>',
    'before_title'  =>  '<h2>',
    'after_title'   =>  '</h2>',
    ));

这是我的sidebar.php:

<ul class="sidebar"> <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('NiceBar') ) : ?> <?php endif; ?> </ul>

然后写入我的index。php:

<?php get_sidebar('NiceBar'); ?>

现在的结果是,虽然我得到一个侧边栏,它只是一个默认的,而不是我的自定义'NiceBar'在前端。在wordpress后端'NiceBar'是注册的,我可以删除它的东西,但它对我的网站没有影响。

欢迎任何建议

你通过名称引用你的侧边栏,而不是你应该通过id:

<?php get_sidebar('sidebar-widget'); ?>

应该可以:

显然也

if ( function_exists('register_sidebar')) {
        register_sidebar(array(
            'name'          =>  'NiceBar',
            'id'            =>  'sidebar-widget',
            'before_widget' =>  '<li>',
            'after_widget'  =>  '</li>',
            'before_title'  =>  '<h2>',
            'after_title'   =>  '</h2>',
            ));
}

sidebar.php

<ul class="sidebar">
       <?php if ( function_exists('dynamic_sidebar')) {
            dynamic_sidebar('sidebar-widget');   
       }  ?>
</ul>

index . php

<?php get_sidebar(); ?>

问题是在get_sidebar()函数中,您告诉在您的主题中有一个文件称为:sidebar-sidebar-widget.php,因为您在get_sidebar()函数中传递参数sidebar-widget。点击这里阅读