0

我遇到了 wordpress Tag CLoud 插件的问题。

我想显示随机标签,但我很困惑如何做到这一点。

代码如下。

我想显示随机标签。就像如果我选择只显示 5 个标签,并且每次我刷新屏幕时标签应该随机出现。

function widget_tagcloud($args){
    $option_value = retrieve_options($opt_val);
    extract($args);
    echo $before_widget;
    echo $before_title;
    echo $option_value['title'];
    echo $after_title;
    global $wpdb;

    $tags_list = get_terms('post_tag', array(
            'orderby'       => 'count',
            'hide_empty'    => 0
             ));

    if(sizeof($tags_list)!=0){
        $max_count = 0;
                if(!empty($option_value['height'])) $canvas_height = $option_value['height'];
                else $canvas_height = "250";
                if(!empty($option_value['width'])) $canvas_width = $option_value['width'];
        else $canvas_width = "250";
        foreach($tags_list as $tag) if($tag->count > $max_count) $max_count = $tag->count;?>
        <div id="myCanvasContainer">
                  <canvas width="<?php echo $canvas_width;?>" height="<?php echo $canvas_height;?>" id="myCanvas">
                    <p>Tags</p>
                  </canvas>
                </div>
                <div id="tags">

        <ul style="
        font-family: <?php if(!empty($option_value['font_name'])) echo $option_value['font_name'];
            else echo "Calibri";?>;
        height:
        <?php 
            if(!empty($option_value['height'])) echo $option_value['height'];
            else echo "250";
        ?>px;
        width:
        <?php 
            if(!empty($option_value['width'])) echo $option_value['width'];
            else echo "250";
        ?>px;
        background-color: #<?php if(!empty($option_value['bg_color'])) echo $option_value['bg_color'];
            else echo "FFF";?>;
        ">
        <?php 
        if(empty($option_value['no_of_tags'])) $option_value['no_of_tags'] = 15;
        if(empty($option_value['txt_color'])) $option_value['txt_color'] = "000";
        if(empty($option_value['max_font_size'])) $option_value['max_font_size'] = 40;
        if(empty($option_value['min_font_size'])) $option_value['max_font_size'] = 3;
        $i=1;
        foreach($tags_list as $tag){
            if($i <= $option_value['no_of_tags']){
                $font_size = $option_value['max_font_size'] - (($max_count - $tag->count)*2);
                if($font_size < $option_value['min_font_size']) $font_size = $option_value['min_font_size'];
                    echo '<li><a href="'.$_SERVER['PHP_SELF'].'?tag='.$tag->slug.'" 
                                                    style="font-size:'.$font_size.'px;color: #'.$option_value['txt_color'].';">'
                                                    .$tag->name.'</a></li>';
                $i++;
                }
            }
            echo '</ul></div>';
        }
    else echo "No tags found";
    echo $after_widget;
}
4

1 回答 1

0

根据您的代码:(尤其是这部分)

$tags_list = get_terms('post_tag', array(
        'orderby'       => 'count',
        'hide_empty'    => 0
         ));

tags你得到的是由 订购的,count因此 - 它们不是随机挑选的。

您可以尝试使用下一个代码:

$tags_list = get_terms('post_tag', array(
        'number'        => 5,
        'orderby'       => 'none',
        'hide_empty'    => 0
         ));

如果这不起作用,请使用 的RAND功能构建自定义查询mysql

编辑:根据您的代码,您可以使用shuffle()php 函数更轻松地进行操作。

只需更换:

$max_count = 0;

和:

$max_count = 5;
shuffle($tags_list);
于 2012-10-15T14:01:22.503 回答