管理菜单中的自定义微件中断微件部分

时间:2012-05-21 作者:Ernie

我正在http://habitat.erniehigh.com 对于客户。我需要添加按类别显示最近帖子的自定义小部件。我在其他地方使用基本相同的代码构建了类似的小部件。

    class habitat_posts extends WP_Widget {
  function habitat_posts(){
$widget_ops = array(
\'description\' => \'Allows you to display a list of recent posts within a particular category.\');
$this->WP_Widget(\'fromcat-widget\', \'Recent posts from a Category\', $widget_ops);
}
 function form($instance) {
//widgetform in backend
$name = $instance[\'title\'];
$numberposts = $instance[\'numberposts\'];
$catid = $instance[\'catid\'];
$rss = $instance[\'rss\'];
?>
 <?php   }  // end of the instance
 function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance[\'catid\'] = $new_instance[\'catid\'];
    $instance[\'numberposts\'] = $new_instance[\'numberposts\'];
    $instance[\'title\'] = $new_instance[\'title\'];
    $instance[\'rss\'] = $new_instance[\'rss\'];
    return $instance;
    } // end the updated
   function widget( $args, $instance ) {
       extract($args, EXTR_SKIP);
         $title = $instance[\'title\'];
         $catid = $instance[\'catid\'];
          $numberposts = $instance[\'numberposts\'];
                  $posts = get_posts(\'numberposts=\'.$numberposts.\'&category=\'.$catid);
                   $out = \'<ul>\';
              foreach($posts as $post) {
              $out .= \'<li><a href="\'.get_permalink($post->ID).\'">\'.$post->post_title.\'</a></li>\';
                 }
          $out .= \'</ul>\';
         echo $before_widget;
         echo $before_title.$title.$after_title;
          echo $out;
          echo $after_widget;
          } // end of widget function
      }  // end the whole process ready to register
         register_widget(\'habitat_posts\');
当我尝试在子主题上使用它时,除了后端的小部件区域乱七八糟之外,一切都正常。新的小部件是可见的,用于创建新实例的表单字段也是可见的,我可以将其拖动到侧栏,但仅此而已。

谢谢你的建议或帮助Ernie

1 个回复
SO网友:brasofilo

我不知道你的代码出了什么问题。。。我知道,如果你按照Codex example, 你会走上正确的道路。

以下是您的示例代码与Codex的合并function form() 缺少字段numberposts、catid和rss,您必须编写此部分
而且,当它工作时,您可以将代码发布在这里作为答案,这样社区就会有一个完全工作的示例。

add_action( \'widgets_init\', create_function( \'\', \'register_widget( "fromcat_widget" );\' ) );

class Fromcat_Widget extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    public function __construct() {
        parent::__construct(
            \'fromcat_widget\', // Base ID
            \'Recent posts from a Category\', // Name
            array( \'description\' => __( \'Allows you to display a list of recent posts within a particular category.\', \'text_domain\' ), ) // Args
        );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
        extract( $args );
        /*
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
        */
        $title = $instance[\'title\'];
        $catid = $instance[\'catid\'];
        $numberposts = $instance[\'numberposts\'];
        $posts = get_posts(\'numberposts=\'.$numberposts.\'&category=\'.$catid);
        $out = \'<ul>\';
        foreach($posts as $post) {
            $out .= \'<li><a href="\'.get_permalink($post->ID).\'">\'.$post->post_title.\'</a></li>\';
        }
        $out .= \'</ul>\';


        echo $before_widget;
        if ( ! empty( $title ) )
            echo $before_title . $title . $after_title;
        echo $out;
        echo $after_widget;
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = array();

        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'numberposts\'] = strip_tags( $new_instance[\'numberposts\'] );
        $instance[\'catid\'] = strip_tags( $new_instance[\'catid\'] );
        $instance[\'rss\'] = strip_tags( $new_instance[\'rss\'] );

        return $instance;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $title = $instance[\'title\'];
        $numberposts = $instance[\'numberposts\'];
        $catid = $instance[\'catid\'];
        $rss = $instance[\'rss\'];
        /*
        if ( isset( $instance[ \'title\' ] ) ) {
            $title = $instance[ \'title\' ];
        }
        else {
            $title = __( \'New title\', \'text_domain\' );
        }*/
        ?>
        <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label> 
        <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 ); ?>" />
        </p>
        <?php 
    }

}

结束