我的小部件代码无法正常工作

时间:2015-08-22 08:47:32

标签: php wordpress

我正在开发WordPress的主题。我想表现出来 小部件区域。在外观区域中显示我的窗口小部件选项 正确但在主屏幕中小部件没有显示 正常。小部件区域显示模板右侧的项目符号点。请任何人帮忙解决我的代码,并提出什么是错误???

我的代码

 register_sidebar(array(  
            'name'=> __('Right Hand Sidebar'),
            'id' => 'right-sidebar',
            'description' => __('Widgets in this area will be shown on the right-hand side'),
            'before_title' => '<h2>',
            'after_title' => '</h2>',
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget' => '</div>'   
));

1 个答案:

答案 0 :(得分:0)

The below code need to add to theme's functions.php.Id should be unique.You need to hook your custom sidebar under widgets_init hook.

<?php 
function register_my_widgets(){
$args = array(
    'name'          => __( 'My Sidebar' ),
    'id'            => 'my-sidebar',
    'description'   => 'This is my sidebar',
        'class'         => '',
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>' ); 
     register_sidebar($args);

     }
     add_action( 'widgets_init', 'register_my_widgets' );
?>


After adding code you easily see your custom sidebar in Appearance->Widgets. Now you can add wordpress default widgets or you own.

To fetch the contents of this sidebar to frontend.You need to add the code below.

<?php 
if ( is_active_sidebar( 'my-sidebar' ) ){

    dynamic_sidebar('my-sidebar');
}

?>