遇到一位客户问到
为什么我的标签有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
即可,最终,呈现的设置效果如下图
完美,搞定。