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);

 

以上就是完整代码,将以上代码,放入functions.php即可,设置好以后呈现的设置效果如下图:

微信扫码咨询
微信扫码咨询
特惠: ¥499元加入终身会员下载所有主题 现在加入
中秋促销: 主题199元起 / 会员400元起 现在加入