blackhill24
blackhill24

Reputation: 452

wordpress get_tags() returning different number of tags each time it runs

The function below is meant to output six tag images randomly selected from an array to the sidebar, this works apart from a small problem.

The problem - each time the function runs it can out put either 5 or 6 tags, i cannot see why this would be.

The only problem i came across which may be related is the if( $count >5 ) needs to be set 1 lower that what is needed.

Any help appreciated, Cheers

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>';
    }    
}

Upvotes: 0

Views: 380

Answers (1)

Ivan Vartanyan
Ivan Vartanyan

Reputation: 69

I'm providing this code for you only to learn by example. I didn't tested it, so it might require debugging.

<?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>';
}

It is better to get 6 random tag ID's before actual tags data retrieval. Also pay attention to the variables naming. Proper names makes your code much more readable. Have a look at the random tags selection algorithm, array_rand do the trick. Best wishes.

Upvotes: 1

Related Questions