Search code examples
phpwordpressunique-id

How to create an unique id for each attachment image in wordpress?


Im trying to create a function for thumbs rotation in wordpress I get everything working but Im stack on the unique id which is required in my js file.

So this is my php function but it doesn't show randomly a different id it shows allways the same id for all thumbnails created.

function thumb_rotation(){
    global $post;
    $uniqueID = 'thumb_';
    $characters = array_merge(range('a','z'), range('0','9'));
    for ($i = 0; $i < 6; $i++) {
    $rand = mt_rand(0, count($characters)-1);
    $uniqueID .= $characters[$rand];
    }
        $images = get_post_meta( $post->ID, 'image_rotator', true );
        $size = 'thumb-video';
        if( $images ):
            foreach( $images as $image ):
                        echo '<span class="mb">';
              echo wp_get_attachment_image($image, $size, false, array('title' => '', 'alt' => '', 'class' => 'mvThumb', 'id' => $uniqueID));
                        echo '</span>';
                endforeach;
            endif;
}

Any idea what Im doing wrong here? Thank You!


Solution

  • Your id generator is outside of your images loop.