这个真的是痛点了,必须给折腾出来,要不然标签云的数量不能控制的话,会导致页面的变长了,不好看了,会影响用户体验了,搞起,搞起。
经过搜索引擎的一番搜索,发现并不难,实际测试后发现,挺好实现的,而且都是有现成的代码案例,直接拿来用就是,不用造轮子的感觉就是好。
/** * 添加新选项到标签云小工具 * @param [type] $widget [description] * @param [type] $return [description] * @param [type] $instance [description] * @return [type] [description] */ function cmhello_tag_cloud_new_options($widget, $return, $instance) { // Are we dealing with a tag_cloud widget? if ('tag_cloud' == $widget->id_base) { ?> <p> <label for="<?php echo $widget->get_field_id('tags_number'); ?>"><?php _e('显示数量:', 'textdomain'); ?></label> <input type="text" class="widefat" id="<?php echo $widget->get_field_id('tags_number'); ?>" name="<?php echo $widget->get_field_name('tags_number'); ?>" value="<?php if (isset($instance['tags_number']) && $instance['tags_number']) echo esc_attr($instance['tags_number']); ?>" /> </p> <p> <label for="<?php echo $widget->get_field_id('orderbytag'); ?>"><?php _e('排序依据:', 'textdomain') ?></label> <select class="widefat" id="<?php echo $widget->get_field_id('orderbytag'); ?>" name="<?php echo $widget->get_field_name('orderbytag'); ?>"> <option <?php if (isset($instance['orderbytag']) && $instance['orderbytag'] == 'name') echo 'selected="SELECTED"'; else echo ''; ?> value="name"><?php echo __('名称', 'textdomain'); ?></option> <option <?php if (isset($instance['orderbytag']) && $instance['orderbytag'] == 'count') echo 'selected="SELECTED"'; else echo ''; ?> value="count"><?php echo __('数量', 'textdomain'); ?></option> </select> </p> <p> <label for="<?php echo $widget->get_field_id('ordertag'); ?>"><?php _e('排序方式:', 'textdomain') ?></label> <select class="widefat" id="<?php echo $widget->get_field_id('ordertag'); ?>" name="<?php echo $widget->get_field_name('ordertag'); ?>"> <option <?php if (isset($instance['ordertag']) && $instance['ordertag'] == 'ASC') echo 'selected="SELECTED"'; else echo ''; ?> value="ASC"><?php echo __('升序', 'textdomain'); ?></option> <option <?php if (isset($instance['ordertag']) && $instance['ordertag'] == 'DESC') echo 'selected="SELECTED"'; else echo ''; ?> value="DESC"><?php echo __('降序', 'textdomain'); ?></option> <option <?php if (isset($instance['ordertag']) && $instance['ordertag'] == 'RAND') echo 'selected="SELECTED"'; else echo ''; ?> value="RAND"><?php echo __('随机', 'textdomain'); ?></option> </select> </p> <?php } } add_filter('in_widget_form', 'cmhello_tag_cloud_new_options', 10, 3); /** * 更新标签云新字段的值 */ function cmhello_tag_cloud_instance($instance, $new_instance, $old_instance) { $instance['tags_number'] = stripslashes($new_instance['tags_number']); $instance['ordertag'] = stripslashes($new_instance['ordertag']); $instance['orderbytag'] = stripslashes($new_instance['orderbytag']); return $instance; } add_filter('widget_update_callback', 'cmhello_tag_cloud_instance', 10, 3); /** * 通过钩子去修改标签云的参数 * @param [type] $args [description] * @param [type] $instance [description] * @return [type] [description] */ function cmhello_tag_cloud_args($args, $instance) { if (isset($instance['tags_number'])) { $args['number'] = $instance['tags_number']; //Limit number of tags } if (isset($instance['orderbytag'])) { $args['orderby'] = $instance['orderbytag']; } if (isset($instance['ordertag'])) { $args['order'] = $instance['ordertag']; } return $args; } add_filter('widget_tag_cloud_args', 'cmhello_tag_cloud_args', 10, 2);