刷新小部件页面后,我的自定义小部件不会停留在小部件区域

时间:2013-09-05 作者:user2619611

正如标题所说,我一直在开发一个简单的图像滚动小部件,现在我几乎完成了,小部件在我拖放并参考页面后不会停留在侧栏区域,知道发生了什么吗?

<?php
/**
 * Plugin Name: Rollover Widget
 * Description: A widget that creates a rollover image effect with a hyperlink.
 * Version: 0.1
 * Author: Zak Elas
 * Author URI: http://dot.com
 */


add_action( \'widgets_init\', \'my_widget\' );


function my_widget() {
    register_widget( \'MY_Widget\' );
}

class MY_Widget extends WP_Widget {

    function MY_Widget() {
        $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that creates a rollover image effect with a hyperlink. \', \'example\') );

        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'Rollover Widget\' );

        $this->WP_Widget( \'example-widget\', __(\'Rollover Widget\', \'example\'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        //Our variables from the widget settings.
        $link =  $instance[\'link\'] ;
        $image = $instance[\'image\'];
        $rollover_image = $instance[\'rollover_image\'];

        echo $before_widget;

        // Display the widget link 

        echo $before_link . $link . $after_link;

        //Display the name 


         echo \'<div id="test">
              <a href="\'.$link.\'">
              <img class="bottom" src="\'.$image.\'" />
              <img class="top" src="\'.$rollover_image.\'" />
              </a>
              </div>\'; 


        echo $after_widget;
    }

    //Update the widget 

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        //Strip tags from link and name to remove HTML 
        $instance[\'link\'] = strip_tags($new_instance[\'link\']);
        $instance[\'image\'] =  strip_tags($new_instance[\'image\']);
        $instance[\'rollover_image\'] = strip_tags( $new_instance[\'rollover_image\']);

        return $instance;
    }


    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'link\' => __(\'Example\', \'example\'), \'image\' => __(\'/images/editorial.png\', \'example\') , \'rollover_image\' => __(\'/images/editorial.png\', \'example\') );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>


        <p>
            <label for="<?php echo $this->get_field_id( \'link\' ); ?>"><?php _e(\'link\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'link\' ); ?>" name="<?php echo $this->get_field_name( \'link\' ); ?>" value="<?php echo $instance[\'link\']; ?>" style="width:100%;" />
        </p>


        <p>
            <label for="<?php echo $this->get_field_id( \'image\' ); ?>"><?php _e(\'image\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'image\' ); ?>" name="<?php echo $this->get_field_name( \'image\' ); ?>" value="<?php echo $instance[\'image\']; ?>" style="width:100%;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( \'rollover_image\' ); ?>"><?php _e(\'rollover_image:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'rollover_image\' ); ?>" name="<?php echo $this->get_field_name( \'rollover_image\' ); ?>" value="<?php echo $instance[\'rollover_image\']; ?>" style="width:100%;" />
        </p>



    <?php
    }
}

?>

1 个回复
最合适的回答,由SO网友:Vinod Dalvi 整理而成

ID Base应该传递给WP\\U Widget类构造函数,如以下代码所示,您传递了错误的ID“example Widget”,它与“ID\\U Base”不匹配,也没有在ID Base中添加空格。用下面修改过的函数替换MY\\u Widget()函数,它就会工作。

function MY_Widget() {
    $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that creates a rollover image effect with a hyperlink. \', \'example\') );

    $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'rollover-widget\' );

    $this->WP_Widget( \'rollover-widget\', __(\'Rollover Widget\', \'example\'), $widget_ops, $control_ops );
}

结束

相关推荐

WordPress帖子中iframe src中的WordPress内容.php文件

我想在帖子中插入一个带有WordPress内容的iframe。我解释我自己。我有两件事:一个帖子和一个演讲。在the_content() WordPress过滤器,我将iframe添加到$内容。此iframe有一个指向自定义的src。php文件,并在其中。php我想访问我的循环,但我无法做到这一点。所以我作弊并通过URL传递帖子的ID,所以我的iframe代码是:<iframe src=\\\"\".WP_PLUGIN_URL.\"/presentation/embedded_presentatio