Strange widget behavior

时间:2012-07-02 作者:2hamed

我通过简单地复制一个默认的widgets类并修改它来创建widget
奇怪的是,小部件管理区域中没有显示此小部件。当我搜索页面源代码时,小部件在那里,但它有内联样式display:none;.<另一件奇怪的事是,标题字段也没有显示
这是我的小部件代码:

/**
 * Mood widget class
 *
**/
class WP_Widget_Mood extends WP_Widget {

    function __construct() {
        $widget_ops = array(\'classname\' => \'widget_mood\', \'description\' => __( "Mood widget") );
        parent::__construct(\'mood\', __(\'Mood\'), $widget_ops);
    }

    function widget($args, $instance){
        extract($args);
        $title = apply_filters(\'widget_title\', empty($instance[\'title\']) ? \'Mood\' : $instance[\'title\'], $instance, $this->id_base ); 

        $user_id = get_current_user_id();

        $table_name = $wpdb->prefix . "mood";

        $get_same=$wpdb->get_row("SELECT * FROM $table_name where user_id=\'".$user_id."\' order by id desc");
        $get_mood_row = mysql_query("select * from wp_mood where user_id=\'".$user_id."\' and mood=\'".$get_same->mood."\'");
        $pathredirect = get_bloginfo(\'url\').\'/m/\'.$current_user->user_nicename.\'/settings/mood/\';
        global $current_user;
        $current_user = wp_get_current_user();

        ?>
        <h3 class="sideheading"><?php echo $title; ?></h3>
        <dl id="settingss" class="dropdown3">
            <dt>
                <a title="Options" href="#setting">
                    <div id="rightsetting" class="sprite"></div>
                </a>
            </dt>
            <dd>
                <ul>
                    <li><p id="wlcmstg2" class="sprite"></p><a href="#Make">Make</a></li>
                    <li><p id="wlcmstg1" class="sprite"></p><a href="<?php echo $pathredirect;?>">Setting</a></li>
                </ul>
            </dd>
        </dl>

        <div id="charectersbar">
            <div id="prev"><span id="go-prev2" class="sprite"> </span></div><!--#prev-->
            <div id="slide">
                <div id="slider2">
                    <?php
                        //echo "select * from wp_mood where user_id!=\'".$user_id."\' and mood=\'".$get_same->mood."\'";
                        $res_mood = mysql_query("select * from wp_mood where user_id=\'".$user_id."\' and mood=\'".$get_same->mood."\'");
                        $ii=0;
                        while($result_mood = mysql_fetch_array($res_mood)){
                            $ii++;
                            $uuserid = $result_mood[\'user_id\'];
                            $user_info = get_userdata($uuserid);
                            ?>
                            <div>
                                <a href="<?php echo bloginfo(\'url\');?>/m/<?php echo $current_user->user_nicename; ?>/profile">
                                    <img title="<?php echo $result_mood[\'nikename\'];?>" src="<?php echo bloginfo(\'url\').\'/wp-content/uploads/mood/\'.$result_mood[\'image\'];?>" /><?php echo $result_mood[\'nikename\'];?>
                                </a> 
                            </div>

                    <?php } ?>
                </div><!--#slider2-->
            </div><!--#slide-->
            <div id="next">
                <span id="go-next2" class="sprite"></span>
            </div>
        </div><!--#charectersbar-->
        <?php
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( \'title\' => \'\') );
        $title = $instance[\'title\'];
?>
        <p><label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\'); ?> <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
<?php
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $new_instance = wp_parse_args((array) $new_instance, array( \'title\' => \'\'));
        $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        return $instance;
    }

}
function mytheme_register_widgets()
    register_widget( \'WP_Widget_Mood\' );
}

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

1 个回复
SO网友:Philip M. Hofer

我没有看到您在此代码中注册小部件。

对于主题内部: register_widget(\'WP_Widget_Mood\');

对于插件内部: add_action( \'widgets_init\', create_function(\'\', \'return register_widget("WP_Widget_Mood");\') );

结束

相关推荐

Widgets in PHP files?

有没有可能让Wordpress上的每个小部件都以不同的方式运行。php文件我有一个包含12个元素的页面,我想让每个元素都成为一个小部件,以便以后更容易管理/编辑它们,但如果它来自php页面,而不是来自管理面板上的代码块,则会更好。