register_sidebar()适用于wordpress后端,但不适用于前端

时间:2015-05-25 17:02:43

标签: php html wordpress

我正在尝试为自定义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'已注册,我可以删除它的内容,但它对我的网站没有任何影响。

非常欢迎任何建议

2 个答案:

答案 0 :(得分:0)

您是按名称引用侧边栏,而应通过ID:

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

答案 1 :(得分:0)

这应该有效:

<强>的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('sidebar-widget');   
       }  ?>
</ul>

<强>的index.php

<?php get_sidebar(); ?>

问题在于get_sidebar()函数,你在主题中有一个名为:sidebar-sidebar-widget.php的文件,因为你在get_sidebar()函数中传递了参数sidebar-widget。 read it here