代码之家  ›  专栏  ›  技术社区  ›  AlexB

Wordpress的自定义小部件可将类别标记为新!

  •  0
  • AlexB  · 技术社区  · 7 年前

    正在尝试为我的简单wordpress博客构建一个小部件,它将在侧栏中显示类别,但与本地wordpress类别小部件不同。基本上,我想实现的是能够将某些类别标记为“新的!”或者类似的东西,但来自小部件本身。

    到目前为止,我有以下代码来注册我的小部件,并可以在后端显示其中的类别列表,在名称旁边带有复选框。

    当我选中该框并试图保存它时,它会再次返回未选中状态。不确定我的更新函数是否实际作为数据库中的序列化数组工作,保存时未更改。

    以下是我迄今为止所做的:

    /* CUSTOM BLOG CATEGORIES WIDGETS */
    class Spr123_Categories_Widget extends WP_Widget {
        public function __construct() {
            $widget_options = array(
                'classname' => 'widget_custom_categories_widget',
                'description' => 'This is a Custom Blog Categories Widget',
            );
            parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
        }
    
        public function form( $instance ) {
            $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
            $name = ! empty( $instance['name'] ) ? $instance['name'] : '';
            $checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
            $categories = get_categories( array(
                'orderby' => 'name',
                'parent'  => 0,
                "hide_empty" => 0,
            ) );
            ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
                <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
            </p>
            <p>
                <label for="Categories">Categories:</label>
            </p>
            <?php print("<pre>".print_r($categories,true)."</pre>"); ?>
            <p>
            <?php
            foreach ( $categories as $category ) {
                ?>
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('checked'); ?>"<?php checked( $checked ); ?> />
                <label for="<?php echo $this->get_field_id($category->slug); ?>"><?php _e( 'Display as NEW - ' . esc_attr( $category->name )); ?></label><br />
                <?php
            }
            ?>
            </p>
            <?php
    
        }
    
        public function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
            $instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
            $instance[ 'checked' ] = !empty($new_instance['checked']) ? 1 : 0;
            return $instance;
        }
    
        public function widget( $args, $instance ) {
    
            $title = apply_filters( 'widget_title', $instance[ 'title' ] );
            $category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
    
            echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
    
            <p><?php echo $category_title ?></p>
    
            <?php echo $args['after_widget'];
        }
    
    }
    function spr123_custom_categories_widget() {
        register_widget( 'Spr123_Categories_Widget' );
    }
    add_action( 'widgets_init', 'spr123_custom_categories_widget' );
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Vel    7 年前

    尝试此代码

      /* CUSTOM BLOG CATEGORIES WIDGETS */
    class Spr123_Categories_Widget extends WP_Widget {
        public function __construct() {
            $widget_options = array(
                'classname' => 'widget_custom_categories_widget',
                'description' => 'This is a Custom Blog Categories Widget',
            );
            parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
        }
    
        public function form( $instance ) {
            $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
            $name = ! empty( $instance['name'] ) ? $instance['name'] : '';
            $checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
    
            $savedcategories = ! empty( $instance['categories'] ) ? $instance['categories'] : '';
    
            $categories = get_categories( array(
                'orderby' => 'name',
                'parent'  => 0,
                "hide_empty" => 0,
            ) );
            ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
                <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
            </p>
            <p>
                <label for="Categories">Categories:</label>
            </p>
    <!--        --><?php //print("<pre>".print_r($categories,true)."</pre>"); ?>
            <p>
            <?php
            foreach ( $categories as $category ) {
                  $checked  = in_array($category->term_id, $savedcategories)?'checked':'';
                ?>
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('categories[]'); ?>" <?php echo $checked; ?> value="<?php echo $category->term_id; ?>"/>
                <label for="<?php echo $this->get_field_id($category->slug); ?>"><?php echo esc_attr( $category->name ) . _e( 'Display as NEW - ' ); ?></label><br />
                <?php
            }
            ?>
            </p>
            <?php
    
        }
    
        public function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
            $instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );        
            $instance[ 'categories' ] = $new_instance['categories'];       
            return $instance;
        }
    
        public function widget( $args, $instance ) {
    
            $title = apply_filters( 'widget_title', $instance[ 'title' ] );
            $category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
    
            echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
    
            <p><?php echo $category_title ?></p>
    
            <?php echo $args['after_widget'];
        }
    
    }
    function spr123_custom_categories_widget() {
        register_widget( 'Spr123_Categories_Widget' );
    }
    add_action( 'widgets_init', 'spr123_custom_categories_widget' );
    

    您可以使用此代码在前端输入值

       $widget_instances = get_option('widget_custom_categories_widget');
    
        print_r($widget_instances);