【问题标题】:Where do I put my custom widget code for my wordpress theme?我应该将我的 wordpress 主题的自定义小部件代码放在哪里?
【发布时间】:2013-09-18 22:55:38
【问题描述】:

我正在为我的 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 wordpress function widget


    【解决方案1】:

    你的 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 }
    

    【讨论】:

    • 谢谢,但是当我在我的 wordpress 管理区域中添加您建议的 php 标签时,所有小部件都消失了,我的小部件不起作用;从某种意义上说,它不能在侧边栏位置拖放。
    • 检查浏览器控制台中的错误,例如是否有任何 JS 错误或检查浏览器视图源中呈现的 html 并仔细检查您是否正确关闭了 html 标签
    • @NeelamKhan Ome 还有一点需要注意的是,您注册了您的小部件吗?喜欢register_widget('widgte class name');试试这个wp.tutsplus.com/tutorials/creative-coding/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2014-01-04
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多