简体   繁体   English

用于Wordpress的自定义小部件,可将类别标记为“新!”

[英]Custom Widget for Wordpress to Enable marking Categories as “New!”

Trying to build a widget for my simple wordpress blog, which will display categories in the sidebar, but not exactly like the native wordpress category widget. 尝试为我的简单wordpress博客构建一个小部件,该小部件将在边栏中显示类别,但与本地wordpress类别小部件并不完全相同。 Basically, what I am trying to achieve is to be able to mark certain categories as "New!" 基本上,我要实现的目标是能够将某些类别标记为“新!”。 or something similar, but from within the widget itself. 或类似的东西,但来自小部件本身。

So far I have the following code that registers my widget and can display categories list in it on the backend with checkbox next to the name. 到目前为止,我已经有了以下代码来注册我的窗口小部件,并可以在后端的名称列表旁边的复选框中显示类别列表。

When I check the box and trying to save it it returns unchecked again. 当我选中该框并尝试保存它时,它再次返回未选中状态。 Not sure if my update function is actually working as serialized array in the DB has not changed on save. 不确定我的更新功能是否实际上在数据库中作为序列化数组工作,保存时未更改。

Here is what I have do far: 这是我到目前为止所做的:

/* 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' );

Try this code 试试这个代码

  /* 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' );

you can the value in frontend by using this code 您可以使用此代码在前端中的值

   $widget_instances = get_option('widget_custom_categories_widget');

    print_r($widget_instances);

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

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