Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 343 karma points
    May 21, 2013 @ 17:12
    Tom Cowling
    0

    Show top 3 nodes

    Hi,

    Could anyone help me construct some xslt to show the top pages under a specific node in the system?

    I've tried searching on here, but people seem to have a problem that's more complex than that or isn't quite the same.

    From that, I can build in the bits I need. I'm basically building a latest news feed for the front page of a site and need to point the xslt to the right node so it can pick the latest three items.

     

    If this information is already out there, then apologies for the doubel post.

     

    Thanks,

    Tom

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 21, 2013 @ 23:33
    Chriztian Steinmeier
    0

    Hi Tom,

    Here's a starter for you - you need to change a couple of aliases to make it work - I've tried to highlight the one's in question:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
    <xsl:variable name="itemsToShow" select="3" />
    
        <!-- Adjust this path if necessary -->
        <xsl:variable name="newsRoot" select="$siteRoot/NewsContainerAlias" />
    
        <xsl:template match="/">
            <xsl:if test="$newsRoot/NewsItemAlias">
                <ul>
                    <xsl:apply-templates select="$newsRoot" />
                </ul>
            </xsl:if>
        </xsl:template>
    
        <xsl:template match="NewsContainerAlias">
            <xsl:for-each select="NewsItemAlias">
                <xsl:sort select="newsDatePropertyAlias" data-type="text" order="descending" />
                <xsl:if test="position() &lt;= $itemsToShow">
    
                    <div class="news" id="news_{@urlName}">
                        <xsl:value-of select="@nodeName" />
                        <!-- ETC. -->
                    </div>
    
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
    
    </xsl:stylesheet>

    (If you haven't got a custom datetime property on them, you can use @createDate instead of newsDateProperyyAlias)

    Feel free to ask 

    /Chriztian 

  • Tom Cowling 144 posts 343 karma points
    May 22, 2013 @ 12:26
    Tom Cowling
    0

    Thanks a lot for this. I do have a custom publish date so that should work well.

    One question though - what should I substitute into the boxes you've highlighted (NewsContainerAlias and NewsItemAlias)? Is it the document type aliases?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 22, 2013 @ 12:48
    Chriztian Steinmeier
    0

    Yep - the aliases (you can see them on the Properties tab, but generally they're Pascal-/camelCased versions of the Name, e.g. "News Container" -> "NewsContainer" for documents and "News Date" -> "newsDate" for properties)

    /Chriztian

  • Tom Cowling 144 posts 343 karma points
    May 22, 2013 @ 12:54
    Tom Cowling
    0

    Hmm.. I think I may hit a problem with that.

    For the landing page, I have used the same document type as the news items, but changed the template so it displays the contents of a macro which loops through and pulls out everything from below and displays in date order. Could I use the alias of the template, or should I create a new document type and then re-create the node?

    Just for future reference, what is usually considered best practice for node levels? I've always been curious as to what other people do and how much effort they put into getting document types right from the beginning.

     

    Thanks for your help so far, by the way!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 22, 2013 @ 13:07
    Chriztian Steinmeier
    0

    No problem Tom!

    You can use it like it is — the way the $newsRoot variable is set up it will select all the NewsContainer nodes that are direct children of the $siteRoot (usually you have only one at this level anyway) - so it will work using the same alias as the NewsItem nodes because they're located below the $newsRoot.

    You have a LOT of flexibility in this - you can match on the specific ID of a node if necessary - but that's generally not very portable as you sure will know.

     

    I'd say it's VERY important to think about how your content structure is going to be before you do any building in Umbraco - they determine the URL structure of your site, so that's very good to "nail" before you've done too much.

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft