wordpress默认的标签云小工具不好用。所以,我这里跟大家分享一下我使用成功了的“标签云”代码,希望对需要的朋友有所帮助。下面就是完整的代码。

<?php
    class iebase_widget_tags extends WP_Widget {

        function __construct() {
            $widget_ops = array( 'classname' => 'iebase_widget_tags' , 'description' => esc_html__( 'Tags' , 'iebase' ) );
            parent::__construct( 'widget_touchsize_tags' ,  esc_html__( 'Tags' , 'iebase' ) , $widget_ops );
        }

        function widget( $args , $instance ) {

            /* prints the widget*/            extract($args, EXTR_SKIP);

            if( isset( $instance['title'] ) ){
                $title = $instance['title'];
            }else{
                $title = '';
            }

            if( isset( $instance['nr_tags'] ) ){
                $nr_tags = $instance['nr_tags'];
            }else{
                $nr_tags = 0;
            }
            echo iebase_var_sanitize($before_widget, 'the_kses');

            if( !empty( $title ) ){
                echo iebase_var_sanitize($before_title . $title . $after_title, 'the_kses');
            }

        ?>
            <!-- panel tags -->
            <div class="tab_menu_content tags-container">
                <?php
                    if($nr_tags != 0){
                        $args = array('number' => $nr_tags, 'orderby' => 'count', 'order' => 'DESC');
                        $tags = get_tags($args);
                    }else{
                        $tags = get_tags();
                    }     
                    if( !empty( $tags ) && is_array( $tags ) ){
                        foreach( $tags as $tag ){
                            $tag_link = get_tag_link( $tag->term_id );
                            ?><a class="tag" href="<?php echo esc_url($tag_link) ?>"> <?php echo airkit_var_sanitize($tag->name, 'esc_attr'); ?></a><?php
                        }
                    }else{
                        echo '<p>' . esc_html__( 'There are no tags.' , 'iebase' ) . '</p>';
                    }
                ?>
            </div>
        <?php
            echo iebase_var_sanitize($after_widget, 'the_kses');
        }

        function update( $new_instance, $old_instance) {

            /*save the widget*/            $instance = $old_instance;
            $instance['title']              = strip_tags( $new_instance['title'] );
            $instance['nr_tags']            = strip_tags( $new_instance['nr_tags'] );

            return $instance;
        }

        function form($instance) {

            /* widget form in backend */            $instance       = wp_parse_args( (array) $instance, array( 'title' => '' , 'nr_tags' => '') );
            $title          = strip_tags( $instance['title'] );
            $nr_tags        = strip_tags( $instance['nr_tags'] );
    ?>

            <p>
                <label for="<?php echo iebase_var_sanitize($this->get_field_id('title'), 'esc_attr'); ?>"><?php esc_html_e('Title','iebase') ?>:
                    <input class="widefat" id="<?php echo iebase_var_sanitize($this->get_field_id('title'), 'esc_attr'); ?>" name="<?php echo iebase_var_sanitize($this->get_field_name('title'), 'esc_attr'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
                </label>
            </p>
            <p>
                <label for="<?php echo iebase_var_sanitize($this->get_field_id('nr_tags'), 'esc_attr'); ?>"><?php esc_html_e( 'Number of tags' , 'iebase' ); ?>:
                    <input class="widefat digit" id="<?php echo iebase_var_sanitize($this->get_field_id('nr_tags'), 'esc_attr'); ?>" name="<?php echo iebase_var_sanitize($this->get_field_name('nr_tags'), 'esc_attr'); ?>" type="text" value="<?php echo esc_attr( $nr_tags ); ?>" />
                    <span class="hint"><?php esc_html_e('Leave blank to show all tags','iebase' ) ?></span>
                </label>
            </p>
    <?php
        }
    }
?>

然后我们注册一下这个小工具

The WordPress Toolbox
Unlimited Downloads: 500,000+ WordPress Themes, Plugins, Templates & Design Assets

主题开发教程:如何自定义标签云小工具

DOWNLOAD NOW

if ( !function_exists( 'iebase_widgets_register' ) ) {
  function iebase_widgets_register() {
    // Register Widgets
    if ( class_exists( 'iebase_widget_tags' ) ) { register_widget( 'iebase_widget_tags' ); }
  }
}
add_action( 'widgets_init', 'iebase_widgets_register' );

 

发表回复