如果创建了自定义小部件,请通过示例创建文件class.my-widget.php
包含自定义小部件的类定义,如下所示:
class my_widget extends WP_Widget {
public function __construct() {
//logic here
}
public function widget($args, $instance) {
//logic here
}
public function form($instance) {
//logic here
}
public function update($new_instance, $old_instance) {
//logic here
}
}
//registering my widget so its available in the back-end
add_action(\'widgets_init\', function() {
register_widget(\'my_widget\');
});
文件位于主题的文件夹中,如下所示:
您必须加载该文件,为此,您必须将其添加到
functions.php
:
//lets define a constant for the URL to your theme folder
define(\'YOUR_THEME_FOLDER_PATH\', trailingslashit(get_template_directory(__FILE__)));
//lets load the custom widget
require_once (YOUR_THEME_FOLDER_PATH . \'inc/class.my-widget.php\');
the
inc/
可以将零件更新到文件所在的文件夹。