Does anyone have any sample code for displaying the latest 5 blog post titles and dates in a list? I'm currently trying to do this via the template 'list sub pages by level' xslt. The level for the blog posts in my installation is 5:
<!-- Input the documenttype you want here --> <xsl:variable name="level" select="5"/>
<xsl:template match="/">
<!-- The fun starts here --> <ul> <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']"> <li> <a href="{umbraco.library:NiceUrl(@id)}"> <xsl:value-of select="@nodeName"/> </a> </li> </xsl:for-each> </ul>
</xsl:template>
</xsl:stylesheet>
However, this returns nothing.
Does anyone know how I can modify this, or alternatively does anyone have some sample XSLT to do what I'm trying to do? It must be quite common to want to list new blog posts on the homepage of a site for example.
The problem you're facing is the use of "$currentPage". Since you want the latest 5 blog posts to be listed from anywhere in the site, you'll need to go up to the root (level=1) node and then back down again.
The solution above works perfectly, thanks Lee. However, I now have an extra requirement which I can't get my head around:
I have two blogs installed on a site, both at the same level but within different parent nodes. How can the code above be adapted to incorporate a source node? I know it must be possible to do, and it's a combination of two of the default XSLT templates (list all pages at a certain level and list all pages from a changeable source) but having tried to combine the source for these, I'm just getting confused.
2. Pass through the "source node id" as a macro parameter. To do this you will need to add a content picker to your macro parameter settings, call it something like "blogNodeId". Then add a variable to your XSLT, like so:
Thanks again Lee - it didn't work with my node structure, but I spent a good few hours last night reading tutorials on XPath and I think I'm starting to get it. Certainly, what I've done this morning, based on your starting point above, has worked perfectly! Thanks again, much appreciated.
XSLT/macro to list latest x blog posts
Hi,
Does anyone have any sample code for displaying the latest 5 blog post titles and dates in a list? I'm currently trying to do this via the template 'list sub pages by level' xslt. The level for the blog posts in my installation is 5:
However, this returns nothing.
Does anyone know how I can modify this, or alternatively does anyone have some sample XSLT to do what I'm trying to do? It must be quite common to want to list new blog posts on the homepage of a site for example.
Thanks all.
Hi Dan,
The problem you're facing is the use of "$currentPage". Since you want the latest 5 blog posts to be listed from anywhere in the site, you'll need to go up to the root (level=1) node and then back down again.
Try this XPath:
Hope that helps?
Cheers, Lee.
Forgot to mention, I'd also add a sort element straight-after the for-each:
This will make sure that the latest blog posts are displayed first, (reverse-chronological order).
The solution above works perfectly, thanks Lee. However, I now have an extra requirement which I can't get my head around:
I have two blogs installed on a site, both at the same level but within different parent nodes. How can the code above be adapted to incorporate a source node? I know it must be possible to do, and it's a combination of two of the default XSLT templates (list all pages at a certain level and list all pages from a changeable source) but having tried to combine the source for these, I'm just getting confused.
Any pointers would be greatly appreciated...
Hi Dan,
You can try the following...
1. Assuming that you only display the latest blog posts within that blog, you can keep the XSLT generic by using:
Set the @level=2 and @level=5 value accordingly.
2. Pass through the "source node id" as a macro parameter. To do this you will need to add a content picker to your macro parameter settings, call it something like "blogNodeId". Then add a variable to your XSLT, like so:
Then in your XPath, use something like this:
That will get all the child nodes from your blog "source node id" and loop through them accordingly!
Good luck, Lee.
Thanks again Lee - it didn't work with my node structure, but I spent a good few hours last night reading tutorials on XPath and I think I'm starting to get it. Certainly, what I've done this morning, based on your starting point above, has worked perfectly! Thanks again, much appreciated.
is working on a reply...