我搜索了很多关于自定义小部件的信息,似乎它可以显示在侧栏中,但我想在主题的页脚中显示它。这只是一个版权自定义文本,我想添加到小部件中,以便在WP admin中轻松更改它。我遵循了本教程:http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/
我有以下结构:
theme-folder
----includes
--------widgets
------------copyright.php
//functions.php
include_once( \'includes/widgets/copyright.php\' );
if (function_exists("register_sidebar")) {
register_sidebar();
}
版权所有。php文件:
<?php
function register_my_widget() {// function to register my widget
register_widget( \'My_Widget\' );
}
function My_Widget() {
$widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that displays the authors name \', \'example\') );
$control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'example-widget\' );
$this->WP_Widget( \'example-widget\', __(\'Example Widget\', \'example\'), $widget_ops, $control_ops );
}
add_action( \'widgets_init\', \'register_my_widget\' );// function to load my widget
class my_widget extends WP_Widget {// The example widget class
// constructor
function __construct() {
parent::__construct(
false,
__( \'My Widget\', \'my-widget\' ),
array( \'description\' => __( \'My sample widget\', \'my-widget\' ) ) );
}
}
function widget( $args, $instance ) {// display the widget
extract( $args );
$title = apply_filters(\'widget_title\', $instance[\'title\'] );
$name = $instance[\'name\'];
$show_info = isset( $instance[\'show_info\'] ) ? $instance[\'show_info\'] : false;
echo $before_widget;
// Display the widget title
if ( $title )
echo $before_title . $title . $after_title;
//Display the name
if ( $name )
printf( \'<p>\' . __(\'Hey their Sailor! My name is %1$s.\', \'example\') . \'</p>\', $name );
if ( $show_info )
printf( $name );
echo $after_widget;
}
function update( $new_instance, $old_instance ) {// update the widget
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'name\'] = strip_tags( $new_instance[\'name\'] );
$instance[\'show_info\'] = $new_instance[\'show_info\'];
return $instance;
}
function form() {// and of course the form for the widget options
//Set up some default widget settings.
$defaults = array( \'title\' => __(\'Example\', \'example\'), \'name\' => __(\'Bilal Shaheen\', \'example\'), \'show_info\' => true );
$instance = wp_parse_args( (array) $instance, $defaults );
//Text Input
// <p>
// <label for="echo $this->get_field_id( \'name\' );"><?php _e(\'Your Name:\', \'example\');</label>
// <input id="echo $this->get_field_id( \'name\' );" name="echo $this->get_field_name( \'name\' );" value="echo $instance[\'name\'];" style="width:100%;" />
// </p>
}
?>
我需要对html进行注释,因为它无法识别,而且我不知道如何调用页脚中的小部件。php,以前有人尝试过这样做吗?