我正在尝试创建一个“关于我的小部件”,如下所示:
<?php
/**
* Plugin Name: About Me Widget
*/
class Custom_About_Me_Widget extends Wp_Widget {
/**
* Setup the Widget
*/
public function __construct() {
$widget_ops = array(\'classname\' => \'widget-about-author\',
\'description\' => __(\'A widget to display information about an author.\')
);
$control_ops = array(\'name\' => \'Enter your name...\',
\'description\' => \'Some words about you...\'
);
parent::__construct( \'_s_about_me_widget\', __(\'_s: About Me Widget\', \'_s_about_me_widget\'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$image = $instance[\'image\'];
$description = $instance[\'description\'];
echo $before_widget;
if( $title ) {
echo $before_title . $title . $after_title;
} else {
echo \'Widget has no title!\';
} ?>
<div class="about-widge">
<?php if( $image ): ?>
<img src="<?php echo esc_url( $image );?>" alt="<?php echo esc_html( $title ); ?>"/>
<?php endif; ?>
<?php if( $description ): ?>
<p><?php echo wp_kses_post( $description ); ?></p>
<?php endif; ?>
</div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'image\'] = strip_tags( $new_instance[\'image\'] );
$instance[\'description\'] = $new_instance[\'description\'];
return $instance;
}
function form( $instance ) {
$defaults = array( \'title\' => \'Your name here...\',
\'image\' => \'\',
\'description\' => \'Enter some words about you...\'
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
?>
<!-- Form for Title -->
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>">Title:</label>
<input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\'];?>" />
</p>
<!-- Form for Image -->
<p>
<label for="<?php echo $this->get_field_id( \'image\' ); ?>">Title:</label>
<input id="<?php echo $this->get_field_id( \'image\' ); ?>" name="<?php echo $this->get_field_name( \'image\' ); ?>" value="<?php echo $instance[\'image\'];?>" />
</p>
<!-- Form for Description -->
<p>
<label for="<?php echo $this->get_field_id( \'description\' ); ?>">Title:</label>
<textarea id="<?php echo $this->get_field_id( \'description\' ); ?>" name="<?php echo $this->get_field_name( \'description\' ); ?>" value="<?php echo $instance[\'description\'];?>" ?>
</textarea>
</p>
<?php
}
}
add_action( \'widget_init\', \'Custom_About_Me_Widget\');
register_widget( \'Custom_About_Me_Widget\' );
?>
不幸的是,这返回了:
Notice
: Undefined index: title in
C:\\xampp\\htdocs\\wordpress\\wp-content\\themes\\_s-master\\inc\\Widgets\\widget_about-author-widget.php
on line
26
对于3个指数中的每一个:
title
,
image
,
description
. 为什么会发生这种情况?