WordPress默认小工具里标签云的数量和排序
为什么我的标签有300多个,而WordPress的小工具里标签云的展示上却只有很少一部分?
我也是很莫名其妙的,不应该啊,查看了后台发现,确实如此,小工具上标签云的展示数量和排序都是无规则的,确实应该有一些可设置的参数,此小工具才更完美啊,话不多说,直接亮代码了。
/** * 添加新选项到标签云小工具 * @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);
代码来自wp大学,将以上代码,放入functions.php
即可,最终,呈现的设置效果如下图

小工具 标签云 参数设定
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)