这个WordPress Widgets API 是如何创建各种小部件和注册侧栏的。
创建新小部件时,可以向任何小部件添加变量。他们从register_sidebars
参数。
args (字符串/数组)(可选)
基于“name”和“id”值构建侧栏。默认值:无
name
- 侧栏名称
id
- 侧栏id.
before_widget
- HTML放在每个小部件之前
after_widget
- HTML放置在每个小部件之后
before_title
- HTML放在每个标题之前
after_title
- HTML放置在每个标题之后。
示例:
<?php
add_action( \'widgets_init\', \'prefix_register_sidebars\' );
function prefix_register_sidebars() {
$args = array(
\'name\' => \'My Sidebar\',
\'id\' => \'my-sidebar\',
\'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',,
\'after_widget\' => \'</div><hr />\',
\'before_title\' => \'<h5 class="widgettitle">\',
\'after_title\' => \'</h5>\'
);
register_sidebars( $args );
}
示例小部件:
class MY_Widget extends WP_Widget {
function my_widget( $args, $instance ) {
$widget_ops = array(
\'description\' => \'My Widget Description\'
);
parent::WP_Widget(false, \'My Widget Name\', $widget_ops );
}
function widget() { // This controls the display of the widget
$title = \'My Widget Title\';
echo $before_widget; // Outputs the the \'before_widget\' register_sidebars setting
echo $title; //Will be wrapped in the \'before_title\' and \'after_title\' settings
echo \'<p>This is my widget output</p>\';
echo $after_widget; //Outputs the \'after_widget\' settings
}
}
add_action( \'widgets_init\', \'prefix_register_widgets\' );
function prefix_register_widgets() {
register_widget( \'my_widget\' );
}