Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Miller 3 posts 83 karma points
    Aug 24, 2016 @ 18:34
    Peter Miller
    0

    Displaying Single Random Image from Media Section

    Hello fellow people,

    i'm new to Umbraco and used to manage the most of things. But now I'm trying to display a Single Random Picture from the Pictures of my Media Section as "a little gimmick" (if you know what I mean)

    I browsed the whole Internet, including this Forum, but none of the solutions worked, so if you could help me, i would be very thankful :)

    Sincerly,

    A desperate User

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Aug 24, 2016 @ 20:45
    Dennis Aaen
    100

    Hi Peter and welcome to our :),

    One way that you could do it is to use this code below.

    @{
        var randomImages = Umbraco.TypedMedia(1064).Children.RandomOrder().Take(1);
        foreach (var image in randomImages)
        {
            <img src="@image.Url" alt="@image.Name" />
        }
    }
    

    Where the ID is the ID of folder from the media section where you want the image taking from.

    If you want the user to pick a folder on a content node using the media picker then you need to use this code.

    @{
        if(Model.Content.HasValue("mediapickeralias")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mediapickeralias")); 
    
             foreach (var image in mediaItem.Children.RandomOrder().Take(1))
            {
                <img src="@image.Url" alt="@image.Name" />
            }
        }   
    }
    

    Remember to change the mediapickeralias so it match your alias for the media picker on the document type.

    Hope this helps,

    /Dennis

  • Peter Miller 3 posts 83 karma points
    Aug 24, 2016 @ 20:59
    Peter Miller
    0

    Thank you so much, i knew it would be a very simple snippet.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies