KNOWLEDGE BASE

Random Messages with XSLT


So you want to display random messages on your Umbraco site but are not sure how to do it?

The following XSLT code will take in a node (in this case 1305) and look at the content randomly choosing one to display. You can easily pass in the node to search by using a Content Picker assigned to an alias of source in the macro parameters. Then simply add the following lines to the xslt below before the <xsl:template match="/"> line.

<xsl:variable name="feed" select="/macro/source"/>

Then change the node id 1305 to $source. Now when you add the macro to a page, you will be prompted to select the parent node to search from.

<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:variable name="numberOfArticleNodes" select="count(umbraco.library:GetXmlNodeById(1305)/* [@isDoc])"/>
<xsl:variable name="randomArticlePosition" select="floor(Exslt.ExsltMath:random() * $numberOfArticleNodes) + 1"/>
<xsl:variable name="randomArticleNode" select="umbraco.library:GetXmlNodeById(1305)/* [@isDoc] [position() = $randomArticlePosition]"/>

<!-- start writing XSLT -->
<div class="cta-text">
<h3><xsl:value-of select="$randomArticleNode/straplineMessage" /></h3>
<p><xsl:value-of select="$randomArticleNode/straplineSubMessage" />.</p>
</div>

</xsl:template>

</xsl:stylesheet>


Need an Umbraco Master?

Here at Simon Antony, we have an in house certified Umbraco Grand Master available for hire. Got a problem with your site, need architecture advice, give us a call to speak to Simon directly and see how we can help

Contact Simon Today!