wordpress get_tags()每次运行时返回不同数量的标签

时间:2018-06-17 09:51:52

标签: php wordpress function if-statement tags

下面的功能是将从数组中随机选择的六个标签图像输出到侧边栏,这与一个小问题不同。

问题 - 每次运行它时,它可以输出5或6个标签,我不明白为什么会这样。

我遇到的唯一可能与之相关的问题是需要将if( $count >5 )设置为低于需要的值。

任何帮助表示赞赏, 干杯

function sidebar_tag_cloud_5416__local_agents() {
    $args = array('include' => '183, 
                                184,
                                182,
                                181,
                                180,
                                179,
                                178,
                                177,
                                176,
                                174,
                                173,
                                258,
                                172,
                                171,


                                '); // List in order of eststate agents page
        $alltags = get_tags( $args );
    echo '<ul id=tag-cloud-sidebar>';
        shuffle($alltags);
        $count=0;
        if ($alltags) {
            foreach($alltags as $tag) {
                $count++;

                    // image id is stored as term meta
                $image_id = get_term_meta( $tag->term_id, 'image', true );

                    // image data stored in array, second argument is which image size to retrieve
                $image_data = wp_get_attachment_image_src( $image_id, 'tag_img' );

                    // image url is the first item in the array (aka 0)
                $image = $image_data[0];

                    if ( ! empty( $image ) ) {
                        echo '<li><a  href="'.get_tag_link($tag->term_id).'">';
                        echo '<img title="' . $tag->name . '" alt="' . $tag->name . '" style="width:160px;" src="' . esc_url( $image ) . '"/>';
                        echo '</a></li>';
                    } 

        if( $count >5 ) break;
        }
            echo '</ul>';
    }    
}

1 个答案:

答案 0 :(得分:1)

我提供此代码只是为了通过示例来学习。 我没有测试过,所以可能需要调试。

False

最好在实际标签数据检索之前获得6个随机标签ID。还要注意变量命名。正确的名称使您的代码更具可读性。看看随机标签选择算法,array_rand做的伎俩。祝福。

相关问题