Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Jan 11, 2010 @ 13:06
    Dan
    0

    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:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!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" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <!-- 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.

    Thanks all.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 11, 2010 @ 13:40
    Lee Kelleher
    0

    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:

    $currentPage/ancestor-or-self::node[@level=1]/descendant::node[@level=$level]/node[string(data[@alias='umbracoNaviHide']) != '1']

    Hope that helps?

    Cheers, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 11, 2010 @ 13:43
    Lee Kelleher
    0

    Forgot to mention, I'd also add a sort element straight-after the for-each:

    <xsl:sort select="@createDate" order="descending" />

    This will make sure that the latest blog posts are displayed first, (reverse-chronological order).

  • Dan 1288 posts 3921 karma points c-trib
    Jan 13, 2010 @ 19:05
    Dan
    0

    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...

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jan 13, 2010 @ 19:37
    Lee Kelleher
    1

    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:

    $currentPage/ancestor-or-self::node[@level=2]/descendant::node[@level=5]/node[string(data[@alias='umbracoNaviHide']) != '1']

    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:

    <xsl:variable name="blogNodeId" select="number(/macro/blogNodeId)" />

    Then in your XPath, use something like this:

    $currentPage/ancestor-or-self::node[@id=$blogNodeId]/descendant::node[@level=5]/node[string(data[@alias='umbracoNaviHide']) != '1']

    That will get all the child nodes from your blog "source node id" and loop through them accordingly!

    Good luck, Lee.

  • Dan 1288 posts 3921 karma points c-trib
    Jan 14, 2010 @ 11:18
    Dan
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft