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即可,最終,呈現的設置效果如下圖

小工具 標籤雲 參數設定
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章