为什么我的自定义小部件中的$实例返回空(未定义的索引)?

时间:2018-01-07 作者:Jonathan Guerin

我正在尝试创建一个“关于我的小部件”,如下所示:

<?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. 为什么会发生这种情况?

1 个回复
SO网友:BearlyDoug

“函数小部件($args,$instance)”不应该是“公共函数小部件($args,$instance)”?参考号:WPBeginner

结束

相关推荐

Using add_filter() in Widgets

只需查询引用add_filter() 作用您可以在WordPress小部件类中使用此函数吗。Example我有一个带有动态创建代码的小部件,我想将其传递给插件中创建的另一个函数。这能实现吗?<?php /** * Display the actual Widget * * @param Array $args * @param Array $instance */ public function widget($args, $inst