开启侧边栏小工具功能
步骤一:在主题的functions.php中,添加一段代码 开启侧边栏功能,代码如下(如果需要多个工具栏,再复…
步骤一:在主题的functions.php中,添加一段代码
开启侧边栏功能,代码如下(如果需要多个工具栏,再复制一个就可以了):
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
 | 
 <?php 
//参数 
$args = array(
 
    ‘name’          => __( ‘主侧边栏’),
 
    ‘id’            => ‘sidebar-01’,
 
    ‘description’   => ‘将在网页中显示的侧边栏’,
 
        ‘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 );
 
 ?>
 
 | 
步骤二:后台给侧边栏添加小工具
步骤三:在模版文件中调用侧边栏
| 
 1 
2 
3 
4 
5 
6 
 | 
 
<?php if ( is_active_sidebar( $index ) ) : ?> //$index 指代哪个侧边栏,可以使用步骤一的name或id值识别
 
     <?php dynamic_sidebar( $index ); ?>
 
<?php else: ?>
 
    //提示用户
 
    //或者,显示一些默认的边栏效果
 
<?php endif; ?>
 
 | 
is_active_sidebar( $index )
判断侧边栏是否被集合,如果激活了,返回true,否则返回false。
dynamic_sidebar( $index )
此函数的做用,是调用$index指定的侧边栏。
实例
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
 
function twentyten_widgets_init() {  //定义后台widgets名称
 
	register_sidebar( array(
 
		‘name’ => __( ‘Primary Widget Area’, ‘twentyten’ ),
 
		‘id’ => ‘primary-widget-area’,
 
		‘description’ => __( ‘The primary widget area’, ‘twentyten’ ),
 
		‘before_widget’ => ‘<li id=”%1$s” class=”widget-container %2$s”>’,
 
		‘after_widget’ => ‘</li>’,
 
		‘before_title’ => ‘<h3 class=”widget-title”>’,
 
		‘after_title’ => ‘</h3>’,
 
	) );
 
} 
//开启侧边栏函数 
add_action( ‘widgets_init’, ‘twentyten_widgets_init’ );
 
 | 
| 
 1 
 | 
 
<?php if(is_dynamic_sidebar()) dynamic_sidebar(‘primary-widget-area’);?> //使用的地方调取
 
 | 
类别:WordPress开发、 
		本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论功能已经关闭!