WordPress函数wp register sidebar widget添加自定义小工具(widget) wordpress


wp register sidebar widget 取代了register_sidebar_widget函数,它提供了函数接口,方便的制作小工具,

用法

<?php 

wp_register_sidebar_widget(

    $id, 

    $name, 

    $output_callback, 

    $options, 

    $params,

    ... 

); 

?>

参数

1.id:               //widget的ID,int、string类型,而且需要唯一

2.name:             //widget显示的明称

3.output_callback:  //当widget被调用时执行的回调函数,该函数返回正式的内容

4.options:          //可选,里面可以放一些要传给widget的参数


举例


1、官网示例

<?php
 function your_widget_display($args) {
   echo $args['before_widget'];
   echo $args['before_title'] . 'My Unique Widget' .  $args['after_title'];
   echo $args['after_widget'];
   // print some HTML for the widget to display here
   echo "Your Widget Test";
}
wp_register_sidebar_widget(
    'your_widget_1',        // your unique widget id
    'Your Widget',          // widget name
    'your_widget_display',  // callback function
    array(                  // options
        'description' => 'Description of what your widget does'
    )
);
?>

注意这就像原生带的小工具一样,可以拖到边栏中使用,但是只能用一次,如果你将其放到边栏框里,那么在选用区就没有了。


2、在边栏临时注册一个广告位

// 首先在主题目录创建一个ads.php文件,里面放生成内容的代码,

// 当然也可以将其写到functions.php中,但是单独出来有利于后期的维护

// ads.php
<?php $options = get_option('xiaohan_options'); 
if ($options['showcase_content']) : ?>
    <div class="rad_c"><?php echo($options['showcase_content']); ?></div>
<?php endif; ?>
// 然后在functions.php中添加下面的代码
if ( ! function_exists( 'ads' ) ) :
        function ads() {
                include(TEMPLATEPATH . '/ads.php');
        }
        wp_register_sidebar_widget( 'ads', __( '广告位', 'ads'), 'ads');
endif;
// 刷新小工具页就能看见了


签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回