Copied to clipboard

Flag this post as spam?

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


  • Thomas Kahn 602 posts 506 karma points
    Jun 04, 2013 @ 15:40
    Thomas Kahn
    0

    Get X latest created images located anywhere inside a specific folder in Media?

    Hi!

    I want to display the 10 (or any number) latest created images located anywhere inside a specific folder in Media. As input i have the ID of the folder in which the images are located and a number for how many images to get. The tricky part is not getting the images if they are located directly in the selected media folder, but getting them if they are somewhere deep down in a nested subfolder. I think some form of recursion is needed but I find it difficult to wrap my head around both recursion and sorting on createDate at the same time.

    The XSLT I have so far looks like this:

    <xsl:variable name="pressImagesFolderId" select="/macro/pressImagesFolderId"/>
    <xsl:variable name="numberOfItems" select="/macro/numberOfItems"/>
    
     
    <xsl:template match="/">
         
     
    <xsl:for-each select="umbraco.library:GetMedia($pressImagesFolderId,1)/Image">
     <xsl:sort select="@createDate" order="descending"/>
     <xsl:if test="position() <= $numberOfItems">
         <xsl:variable name="image" select="./umbracoFile"/>
         <img>
             <xsl:attribute name="src">
                 <xsl:value-of select="$image"/>
             xsl:attribute>
         <img>
     <xsl:if>
    </xsl:for-each>
    
    <xsl:template>

    Is it possible to do what I'm trying to do with XSLT and if so, what would it look like?

    Thanks in advance!
    /Thomas Kahn

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jun 04, 2013 @ 16:08
    Chriztian Steinmeier
    100

    Hi Thomas,

    You're pretty close, actually - the key is getting all the descendant images - that's what the double-slash does - otherwise you we're cool; my changes are pretty much just semantics:

    <xsl:variable name="pressImagesFolderId" select="/macro/pressImagesFolderId"/>
    <xsl:variable name="numberOfItems" select="/macro/numberOfItems"/>
    
    <xsl:variable name="folderNode" select="umbraco.library:GetMedia($pressImagesFolderId, 1)" />
    
    <xsl:template match="/">
        <xsl:for-each select="$folderNode//Image">
            <xsl:sort select="@createDate" order="descending" />
            <xsl:if test="position() &lt;= $numberOfItems">
                <img src="{umbracoFile}" width="{umbracoWidth}" height="{umbracoHeight}" alt="{@nodeName}" />
            </xsl:if>
        </xsl:for-each>
    <xsl:template>

    /Chriztian

  • Thomas Kahn 602 posts 506 karma points
    Jun 04, 2013 @ 16:31
    Thomas Kahn
    0

    Wow! Thanks Chriztian!
    I had no idea I could use the double slash like this when working with the media section.

    Just one modification to the script - just a syntax thing ($numberOfItems need to be interpreted as a number):

     <xsl:variable name="pressImagesFolderId" select="/macro/pressImagesFolderId"/>
     <xsl:variable name="numberOfItems" select="/macro/numberOfItems"/>
    
     <xsl:variable name="folderNode" select="umbraco.library:GetMedia($pressImagesFolderId, 1)" />
     <xsl:template match="/">
    
    
     <xsl:for-each select="$folderNode//Image">
     <xsl:sort select="@createDate" order="descending"/>
     <xsl:if test="position() <= number($numberOfItems)">
     <img src="{umbracoFile}" width="{umbracoWidth}" height="{umbracoHeight}" alt="{@nodeName}" />
     <xsl:if>
    <xsl:for-each>
    <xsl:template>

    Thanks a million! Your answer saved me a lot of headaches and frustration!

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jun 04, 2013 @ 16:35
    Chriztian Steinmeier
    0

    No problem!

    I've never had to use the number() function for situations like that - curious if you were having problems without it?

    XSLT usually does the conversion that makes most sense... (since everything in XML is a string...)

    /Chriztian

  • Thomas Kahn 602 posts 506 karma points
    Jun 04, 2013 @ 16:37
    Thomas Kahn
    0

    Yes, I was getting an error if I omitted the number() function. It must be because the value comes from a macro parameter which is a string.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jun 04, 2013 @ 17:12
    Chriztian Steinmeier
    0

    Hehe - I'm willing to bet you a beer that there's an accidental extra space in that macro parameter then :-)

    Nevermind - it works! On to the next problem :-)

    /Chriztian  

Please Sign in or register to post replies

Write your reply to:

Draft