您现在的位置是:首页 > 网站制作 > WordpressWordpress

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

杰帅2023-06-26【Wordpress】人已围观

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

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

用法

<?php 
wp_register_sidebar_widget(
    $id, 
    $name, 
    $output_callback, 
    $options, 
    $params,
    ... 
); 
?>

参数

id: //widget的ID,int、string类型,而且需要唯一
name: //widget显示的明称
output_callback: //当widget被调用时执行的回调函数,该函数返回正式的内容
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/

标签 :  ,  ,  , 

本文版权归原作者所有,转载请注明原文来源出处, WordPress教程网 感谢您的支持!

Tags:wordpress函数   wordpress教程   wp register sidebar widget   自定义小工具

很赞哦! ()

文章评论

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计1172篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们