I'm new to umbraco and xslt and really struggling with this. I'm trying to retrieve all event items for a site and display the two newest in a summary control.
I can retrieve the items but due to be retrieving items with a position less than 3 its ignoring the sort.
It's not ignoring the sort - it's just sorting the wrong nodes - the way you need to do it is to first sort all the nodes and then only do something to the first two:
Sort descending when retrieving x number of items
I'm new to umbraco and xslt and really struggling with this. I'm trying to retrieve all event items for a site and display the two newest in a summary control.
I can retrieve the items but due to be retrieving items with a position less than 3 its ignoring the sort.
<xsl:template match="/">
<xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::node [@level = 1 and @nodeTypeAlias = 'Home']" />
<xsl:variable name="Items" select="$rootNode/descendant::node [@nodeTypeAlias = 'Event_Item']" />
<div class="events-summary">
<xsl:for-each select="$Items [position() < 3]">
<xsl:sort select="@createDate" order="descending" />
<div class="item">
<div class="item-left">
<img>
<xsl:attribute name="src">
<xsl:value-of select="data [@alias = 'primaryImage']"/>
</xsl:attribute>
</img>
</div>
<div class="item-right">
<span class="date"><xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'dd.MM.yy')"/></span><br />
<xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(data [@alias = 'primaryContent']), 80, '...')" />
<a href="{umbraco.library:NiceUrl(@id)}">Read More...</a>
</div>
</div>
</xsl:for-each>
<div class="clear"> </div>
</div>
</xsl:template>
Pretty close, just need to move the position() < 3 to an if after the sort.
<xsl:for-each select="$Items">
Hi Craig,
It's not ignoring the sort - it's just sorting the wrong nodes - the way you need to do it is to first sort all the nodes and then only do something to the first two:
/Chriztian
Hi Jeff and Chriztian,
Just tried the code and worked like a charm. I was stuck on this for about 3 hours last night and really do appreciate your help.
Thanks
Craig
is working on a reply...