Nibble

XSLT List x most recent documents

Something you see on loads of site homepages is a list of the x most recent newsitems.

In the umbraco backend you would have a structure simular to this example:

xslt

A Homepage with different types of documents underneath and somewhere in that underlying structure you will also have newsitems documents.

So how do we get a list of the 5 most recent newsitems on the homepage.

Take a look at the example below, I started from the “list sub pages from current page template” in umbraco and added a couple of lines.

First I created a variable that will hold the number of items to display. Then I adjusted the select in the for-each loop to only get the child nodes of NewsPage documents.

Next I sort the items on createDate and the final code I added is an if condition to check if the current position in the loop is lower or equal to the variable (itemsToDisplay).

 

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE xsl:Stylesheet [ &lt;!ENTITY nbsp “&#x00A0;”> ]>
<xsl:stylesheet
version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:msxml=”urn:schemas-microsoft-com:xslt”
xmlns:umbraco.library=”urn:umbraco.library”
exclude-result-prefixes=”msxml umbraco.library”>
 
 
<xsl:output method=”xml” omit-xml-declaration=”yes”/>
 
<xsl:param name=”currentPage”/>
 
<xsl:variable name=”itemsToDisplay” select=”5″ />
 
<xsl:template match=”/”>
 
<!– The fun starts here –>
<ul>
<xsl:for-each select=”$currentPage/descendant::node [@nodeTypeAlias = ‘NewsPage’]/node”>
<xsl:sort select=”@createDate” order=”descending”/>
 
<xsl:if test=”position() &lt;= $itemsToDisplay”>
<li>
<a href=”{umbraco.library:NiceUrl(@id)}”>
<xsl:value-of select=”@nodeName”/>
</a>
</li>
</xsl:if>
</xsl:for-each>
</ul>
 
</xsl:template>
 
</xsl:stylesheet>

4 Comments so far

  1. zuppaman on April 16th, 2008

    Hey Tim,

    Ik probeerde je te contacteren via je Hotmail adres zonder resultaat.
    Kan ik je op een ander adres bereiken.

    Thx !

  2. Tim Geyssens on April 17th, 2008

    Heb je mailtje gezien, stond bij de ongewenste, antwoord net verzonden…

    Groeten,
    Tim

  3. max on January 9th, 2012

    Hi I have a list of sub pages that is generated randomly using a random function but i need 1 page to be on top as feature page .. but the rest to be at randomly generated some xslt examples would be very useful..

  4. dr memals on August 1st, 2012

    this is what I was looking for but as well as the link to the sub page I need a preview (first 100 words) from the sub page content field. can XSLT do this ?

Leave a Reply