WordPress函数wp register sidebar widget添加自定义小工具(wid
wp register sidebar widget 取代了register_sidebar_widget函数,它提供了函数接口,方便的制作小工具, 用法 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、官网示例 ? 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 if ($options['showcase_content']) : ?> // 然后在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/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |