One single random image/picture from a media folder
Hi,
I don't know why, but I can't get the random image to work at all. All the solutions I found here in the forum says Value was either too large or too small for an Int32.
I use the following XSLT code to pick up the media folder:
Try adding this string to your resulting page with the error: ?umbDebugShowTrace=true - now you'll get some debugging information, which can maybe help you.
If there are some errors in the XSLT it will be highlighted in red. You can't miss it.
Please feel free to post the error in here if it gives you any.
The problem is not so much with the xslt but with the fact that this xslt is not designed to work directly against the media section. I would guess it was designed to get images saved in the content tree that were associated with an upload datatype.
The two approaches are similar but not the same.
Since you said you had 4 images in a folder in the MEDIA section, this would be the way to handle it...
Get all the images below the $bannerFolder, whose ID you've pre-defined in the $bannerFolder variable. Because the GetMedia() call is relatively slow we'll save the output to a variable called banerFolderNodes that contains all the data we'll later need.
We then count the number of nodes and create a random number between 1 and the number of nodes in the media folder.
Then we select a single, random node from all of the images in the $bannerFolderNodes list, saving it to the $image variable.
At this point the work is basically done and we only need to create the <img /> tag, by getting the 'umbracoFile' information from the image node.
Thanks both for the reply. I was trying out Douglas version and it just work like a charm :) I actually think it was your code I used at first Douglas, if I'm not wrong.
<xsl:template match="/">
//this is where you set your default image folder <xsl:variable name="mediaFolderID" select="1074" /> <xsl:if test="$mediaFolderID > 0"> <xsl:variable name="mediaID" select="umbraco.library:GetMedia($mediaFolderID, 1)" /> <xsl:if test="count($mediaID//Image) > 0"> <xsl:for-each select="$mediaID//Image"> <xsl:sort select="randomTools:GetRandom(0,count($mediaID//Image))" order="ascending" /> <xsl:if test="position() = 1"> <img src="{umbracoFile}" alt="" width="{umbracoWidth}" height="{umbracoHeight}" /> </xsl:if> </xsl:for-each> </xsl:if> </xsl:if> </xsl:template>
<msxsl:script language="c#" implements-prefix="randomTools"> <msxsl:assembly href="../bin/umbraco.dll"/> <![CDATA[ /// <summary> /// Gets a random integer that falls between the specified limits /// </summary> /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param> /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param> /// <returns>A random integer within the specified range</returns> public static int GetRandom(int lowerLimit,int upperLimit) { Random r = umbraco.library.GetRandom(); int returnedNumber = 0; lock (r) { returnedNumber = r.Next(lowerLimit, upperLimit); } return returnedNumber; } ]]> </msxsl:script> </xsl:stylesheet>
Here's an example based on Owens code with the addition of links on the images (create a new datatype called Header with the attributes of the image datatype and then add the "headerLink" property under "Generic properties" as a textfield. Or just add the headerLink property to the existing image datatype
<msxsl:script language="c#" implements-prefix="randomTools"> <msxsl:assembly href="../bin/umbraco.dll"/> <![CDATA[ /// <summary> /// Gets a random integer that falls between the specified limits /// </summary> /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param> /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param> /// <returns>A random integer within the specified range</returns> public static int GetRandom(int lowerLimit,int upperLimit) { Random r = umbraco.library.GetRandom(); int returnedNumber = 0; lock (r) { returnedNumber = r.Next(lowerLimit, upperLimit); } return returnedNumber; } ]]> </msxsl:script> </xsl:stylesheet>
Your script worked perfectly for me 4.7 installation of Umbraco. Now, instead of displaying an actual image, i converted this into creating a style property, if you for example would want a random background.
I used Owen's adaptation of Douglas's code with 4.7 and it worked although there were three things;
The comment "//this is where you set your default image folder" showed up on the final page until I removed it entirely, adding an extra slash didn't remove it, but I imagine this is just a syntax problem
Changing the "<xsl:variablename="maxItems"select="number(25)"/>" number didn't seem to affect anything but then the variable doesn't seem to be used elsewhere in the file, so I'm not sure why it's there.
Sometimes no image will be shown
I can live with the first two but the last makes the whole thing less useful as there's not even a place holder just a full stop instead. Does anybody know about this last bit?
One single random image/picture from a media folder
Hi,
I don't know why, but I can't get the random image to work at all. All the solutions I found here in the forum says Value was either too large or too small for an Int32.
I use the following XSLT code to pick up the media folder:
The number 1264 is picked from the Media Archive:
- Media Archive
- Background picture (1264)
There is 4 pictures in that folder at the moment
I don't know what I should do to remove this error. Anyone who can see what the problem could be?
I'm running Umbraco 4.0.3
Kind regards,
Niels
Tick the "Skip testing" checkbox, this is a common problem as the editor cannot validate one of the values.
More details and workaround here http://forum.umbraco.org/yaf_postst3143_NiceUrl-failing-on-valid-node.aspx
If I do that, instead I get the following error:
Hi Niels
Try adding this string to your resulting page with the error: ?umbDebugShowTrace=true - now you'll get some debugging information, which can maybe help you.
If there are some errors in the XSLT it will be highlighted in red. You can't miss it.
Please feel free to post the error in here if it gives you any.
/Jan
Hi Jan,
Looking there is no error there related to the background front img.
Here is the link for the testsite: http://red.nikri.dk. I have turned of the existing static background picture.
/Niels
Hi Jan,
There is no error related to the background front img.
Here is the link for the testsite: http://red.nikri.dk . I have turned of the existing static background picture.
/Niels
Hi, Niels,
The problem is not so much with the xslt but with the fact that this xslt is not designed to work directly against the media section. I would guess it was designed to get images saved in the content tree that were associated with an upload datatype.
The two approaches are similar but not the same.
Since you said you had 4 images in a folder in the MEDIA section, this would be the way to handle it...
The code does the following...
Get all the images below the $bannerFolder, whose ID you've pre-defined in the $bannerFolder variable. Because the GetMedia() call is relatively slow we'll save the output to a variable called banerFolderNodes that contains all the data we'll later need.
We then count the number of nodes and create a random number between 1 and the number of nodes in the media folder.
Then we select a single, random node from all of the images in the $bannerFolderNodes list, saving it to the $image variable.
At this point the work is basically done and we only need to create the <img /> tag, by getting the 'umbracoFile' information from the image node.
Hope this helps.
cheers,
doug.
Hello,
I think this will solve your problem:
I changed the GetXmlNodeById for the GetMedia in the count nodes, and then output directly that node.
Hope that it solves your problem.
Paulo
Hi Paulo & Douglas,
Thanks both for the reply. I was trying out Douglas version and it just work like a charm :) I actually think it was your code I used at first Douglas, if I'm not wrong.
Again, thanks a lot for the help from all of you.
Kind regards,
Niels
Hi
I try to get this code working with the 4.5 schema, but I can't get this get right.
Can someone help?
Thanks a lot
Kind regards
Try this implementation I just used it in my 4.5.2 install:
Goodluck!
Owen
Thanks a lot! It's working flawless!
Owen, you're a star.
Here's an example based on Owens code with the addition of links on the images (create a new datatype called Header with the attributes of the image datatype and then add the "headerLink" property under "Generic properties" as a textfield. Or just add the headerLink property to the existing image datatype
Sorry, MediaType under "Settings", not Datatype.
Thanks Owen,
Your script worked perfectly for me 4.7 installation of Umbraco.
Now, instead of displaying an actual image, i converted this into creating a style property, if you for example would want a random background.
I changed the:
Into:
I hope this may someone else looking for a solution on how to do this! :) Have a nice evening everyone.
// Thomas G
I used Owen's adaptation of Douglas's code with 4.7 and it worked although there were three things;
I can live with the first two but the last makes the whole thing less useful as there's not even a place holder just a full stop instead. Does anybody know about this last bit?
I tried it out with the code of Owen. I'm using Umbraco 4.10.11.
Code for random background of the body tag.
Code:
Step 1: Go to Backend -> Developer -> XSLT Files -> right click create...(activate: create Macro), then paste the code in.
Step 2: Go to Backend -> Settings -> Templates -> Master -> into theyou to set the Macro of the XSLT you created in Step 1.
example:
They #body { } in your CSS file could be empty.
Now, you got a random background if you are clicking arround on the webpage.
That's how I did this. I'm new to Umbraco, perhaps there is an other way to use this better, but for me it is perfect.
best regards
is working on a reply...