繁体   English   中英

如何在我的wordpress主题中放置自定义窗口小部件代码?

[英]Where do I put my custom widget code for my wordpress theme?

我正在为我的wordpress主题创建自定义窗口小部件。 我的代码在某些时候中断了。

我不想将小部件打包为插件,因此我将代码放入functions.php文件中。

这是我的代码:

class gmp_widget extends WP_Widget {
         function gmp_widget() {
               // widget actual processes

               $widget_ops = array('classname' => 'gmp_widget', 'description' => __('Example widget that displays a user\'s bio.','gmp-plugin'));
               $this->WP_Widget('gmp_widget_bio', __('Bio Widget','gmp-plugin'), $widget_ops);

        }

        public function form( $instance ) {
               // outputs the options form on admin
               $defaults = array( 'title' => __('My Bio', 'gmp-plugin'), 'name' => '', 'bio' => '');
               $instance = wp_parse_args( (array) $instance, $defaults);
               $title = strip_tags($instance['title']);
               $name = strip_tags($instance['name']);
               $bio = strip_tags($instance['bio']);

               <p><?php _e('Title', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
               <p><?php _e('Name', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('name'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
               <p><?php _e('Bio', 'gmp-plugin') ?>: <textarea  class="widefat" name="<?php echo $this->get_field_name('bio'); ?>" <?php echo esc_attr($title); ?></textarea></p>

        }

        public function update( $new_instance, $old_instance ) {
               // processes widget options to be saved
               $instance = array();
               $instance['title'] = strip_tags($new_instance['title']);
               $instance['name'] = strip_tags($new_instance['name']);
               $instance['bio'] = strip_tags($new_instance['bio']);

               return $instance;
        }

        public function widget( $args, $instance ) {
               // outputs the content of the widget
               extract($args);

               echo $before_widget;

               $title = apply_filters('widget_title', $instance['title'] );
               $name = empty($instance['name']) ? '&nbsp;' :
               apply_filters('widget_name', $instance['name']);
               $bio = empty($instance['bio']) ? '&nbsp;' :
               apply_filters('widget_bio', $instance['bio']);

               if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
               echo '<p>' .__('Name', 'gmp-plugin') .': ' . $name . '</p>';
               echo '<p>' .__('Bio', 'gmp-plugin') .': ' . $bio . '</p>';
               echo $after_widget;



              }

}

当到达函数形式($ instance)时,我可以看到p标记给出了语法错误,因为下面的所有代码在文本编辑器中从看起来正确,所有正确的颜色变为黑色,这告诉我有一个问题某处,但我不知道这是什么。 任何帮助将不胜感激! 提前致谢!

您对php标签有疑问,必须关闭php标签然后像这样启动html

   public function form( $instance ) {
           // outputs the options form on admin
           $defaults = array( 'title' => __('My Bio', 'gmp-plugin'), 'name' => '', 'bio' => '');
           $instance = wp_parse_args( (array) $instance, $defaults);
           $title = strip_tags($instance['title']);
           $name = strip_tags($instance['name']);
           $bio = strip_tags($instance['bio']); ?> //close php tag here 
// Now put your html
           <p><?php _e('Title', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
           <p><?php _e('Name', 'gmp-plugin') ?>: <input class="widefat" name="<?php echo $this->get_field_name('name'); ?>" type="text" value="<?php echo esc_attr($name); ?>"</p>
           <p><?php _e('Bio', 'gmp-plugin') ?>: <textarea  class="widefat" name="<?php echo $this->get_field_name('bio'); ?>" <?php echo esc_attr($title); ?></textarea></p> //end of html
// Now open php tag
    <?php }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM