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

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;
// 刷新小工具页就能看见了

官网:https://developer.wordpress.org/reference/functions/wp_register_sidebar_widget/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值