我要投稿 | 网站地图 WordPress建站学习平台
WordPress教程网
热门标签: wordpress教程 wordpress函数 wordpress WordPress建站 wordpress插件

图文教程

当前位置:首页> 图文教程

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

发布:WordPress教程网   发布时间: 2023-03-08  游览次数:14次

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教程网 感谢您的支持!

本文链接: http://www.wpcn.net/1800.html

WordPress教程网
WordPress教程网
相关内容