A Latest News Macro using XSLT,
The Following Code can be placed in a Template or in a Macro for a user to place on any page.
What this Macro does, is display only the last 2 news items, I have used it on the following sites, for example:
Cindy-Lou photographer (link to follow) and SCSC (link to follow)
The only bit that needs cahnged per project are:
1) <xsl:variable name="maxItems" select="2"/> (highlighted below) --> This ensures only the lates 2 News Items are shown.
2) <xsl:if test="@template = 1169"> (highlighted below) --> 1169 is the Template ID of my News Articles, change 1169 to match the News Item template for your project.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<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:PS.XSLTsearch="urn:PS.XSLTsearch"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets PS.XSLTsearch ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<!-- update this variable on how deep your site map should be -->
<xsl:variable name="maxLevelForSitemap" select="8"/>
<xsl:variable name="maxItems" select="2"/>
<xsl:template match="/">
<div id="latestNews">
<xsl:variable name="counter" select="1" />
<h2>Latest News</h2>
<ul>
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
</xsl:call-template>
</ul>
</div>
</xsl:template>
<xsl:template name="drawNodes">
<xsl:param name="parent"/>
<xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
<xsl:for-each select="$parent/* [@isDoc and @level <= $maxLevelForSitemap]">
<xsl:sort select="newsDate" order="descending"/>
<xsl:if test="position()<= $maxItems">
<xsl:if test="@template = 1169">
<li>
<a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="newsTitle"/></a>
</li>
</xsl:if>
</xsl:if>
<xsl:if test="count(./* [@isDoc and @level <= $maxLevelForSitemap]) > 0">
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
What this Macro does, is display only the last 2 news items, I have used it on the following sites, for example:
Cindy-Lou photographer (link to follow) and SCSC (link to follow)
The only bit that needs cahnged per project are:
1) <xsl:variable name="maxItems" select="2"/> (highlighted below) --> This ensures only the lates 2 News Items are shown.
2) <xsl:if test="@template = 1169"> (highlighted below) --> 1169 is the Template ID of my News Articles, change 1169 to match the News Item template for your project.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<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:PS.XSLTsearch="urn:PS.XSLTsearch"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets PS.XSLTsearch ">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<!-- update this variable on how deep your site map should be -->
<xsl:variable name="maxLevelForSitemap" select="8"/>
<xsl:variable name="maxItems" select="2"/>
<xsl:template match="/">
<div id="latestNews">
<xsl:variable name="counter" select="1" />
<h2>Latest News</h2>
<ul>
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc and @level=1]"/>
</xsl:call-template>
</ul>
</div>
</xsl:template>
<xsl:template name="drawNodes">
<xsl:param name="parent"/>
<xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
<xsl:for-each select="$parent/* [@isDoc and @level <= $maxLevelForSitemap]">
<xsl:sort select="newsDate" order="descending"/>
<xsl:if test="position()<= $maxItems">
<xsl:if test="@template = 1169">
<li>
<a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="newsTitle"/></a>
</li>
</xsl:if>
</xsl:if>
<xsl:if test="count(./* [@isDoc and @level <= $maxLevelForSitemap]) > 0">
<xsl:call-template name="drawNodes">
<xsl:with-param name="parent" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Comments
Post a Comment