代码之家  ›  专栏  ›  技术社区  ›  blackhill24

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

  •  0
  • blackhill24  · 技术社区  · 6 年前

    下面的函数用于将从一个数组中随机选择的六个标记图像输出到侧边栏,这除了一个小问题外还可以工作。

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

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

    感谢您的帮助, 干杯

    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 回复  |  直到 6 年前
        1
  •  1
  •   Ivan Vartanyan    6 年前

    我提供这段代码只是为了以身作则。 我没有测试它,所以可能需要调试。

    <?php
    
    function sidebar_tag_cloud_5416__local_agents() {
        // List in order of estate agents page
        $selected_agents = [183, 184, 182, 181, 180, 179, 178, 177, 176, 174, 173, 258, 172, 171];
        $tags_count = 6;
    
        // Let's select 6 random tag ID's
        $selected_agents = array_intersect_key($selected_agents, array_flip(array_rand($selected_agents, $tags_count)));
        $selected_agents = array('include' => implode(',', $selected_agents));
    
        // Retrieving tags
        $tags = get_tags($selected_agents);
    
        // List output
        echo '<ul id="tag-cloud-sidebar">';
    
        if (!empty($tags)) {
            foreach($tags as $tag) {
                // 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>';
                }
            }
        }
    
        echo '</ul>';
    }
    

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