Copied to clipboard

Flag this post as spam?

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


  • Garry Bain 149 posts 124 karma points
    Oct 22, 2009 @ 16:37
    Garry Bain
    0

    Creating a home page news item

    Hi everyone,

    I currently have 2 documents types for my news section:

    1. News - this shows an overview of all published news items
    2. NewsItem - this is the actual news item itself

    However, I wish to have a section on my homepage where I can show the teaser from the latest published news item. I have a teaser field called "newsTeaser" inside my NewsItem document type - but I'm unsure how to show this on my homepage in my main template.

    Any help would be great, Garry.

  • Folkert 82 posts 212 karma points
    Oct 22, 2009 @ 16:41
    Folkert
    0

    Just create an xslt file where you get the latest news item and insert the macro on your homepage template.

  • dandrayne 1138 posts 2262 karma points
    Oct 22, 2009 @ 17:24
    dandrayne
    2

    Here's a similar script I've used to display the latest 3 news on the homepage

    Pay close attention to the sort, the limiting to 3 and the properties used.  You'll have to change these!  This also assumes a property called newsDate with a datepicker, but you could just use the publish date

    <?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"
    exclude-result-prefixes="msxml umbraco.library">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
    <xsl:variable name="datenow" select="umbraco.library:CurrentDate()" />
    <xsl:variable name="newsPage" select="umbraco.library:GetXmlNodeById(/macro/newsPage)" />
    <ul id="homepage_news">
    <xsl:for-each select="$newsPage//node [string(data [@alias='newsDate']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">
    <xsl:sort select="current()/data[@alias='newsDate']" order="descending" />

    <xsl:if test="position() &lt;= 3">
    <li>
    <h3>
    <a title="View full story of {@nodeName}" href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="./data [@alias = 'newsHeadline']"/>
    </a>
    </h3>
    <p><xsl:value-of select="./data [@alias = 'newsSummary']"/></p>
    </li>

    </xsl:if>
    </xsl:for-each>
    </ul>


    </xsl:template>

    </xsl:stylesheet>

    Dan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 22, 2009 @ 17:27
    Jan Skovgaard
    0

    Hi Dan

    Great example. Have you put it into the wiki? If not I would encourage you to do it :)

    /Jan

  • dandrayne 1138 posts 2262 karma points
    Oct 22, 2009 @ 18:10
    dandrayne
    0

    @Jan Good point.  I have a lot of little snippets like that (as I'm sure lots of devs do), that would really be great in the wiki.  I'll try to get time to genericise them and upload.  It would help stop these little snippets getting lost on the forum.

    @garry, I should note that the snippet above requires a content picker as a macro paramater to select which news page to take the news items from.  In your case you migth want to grab all news items below the homepage, in which case get rid of

    <xsl:variable name="newsPage" select="umbraco.library:GetXmlNodeById(/macro/newsPage)" />

    and change


    <xsl:for-each select="$newsPage//node [string(data [@alias='newsDate']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">

    to


    <xsl:for-each select="$currentPage//node [string(data [@alias='newsDate']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">

     

    Hope this helps,
    Dan

  • Garry Bain 149 posts 124 karma points
    Oct 26, 2009 @ 17:50
    Garry Bain
    0

    Hi guys,

    I've been away over the weekend so only just had chance to check the post. Thanks for all the replies, I'm currently trimming down your reply Dan, but there is a structural issue concerning my XSLT. The actual news item and teaser only appear on my news page - and not from my home page. So it seems this is working - but theres some code missing - or an incorrect parameter for this to display on the homepage. Here is my XSLT if you can spot where I'm going wrong - I presume its something to do with $currentPage as it is not searching through the page structure properly:<code>

    <?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"
            exclude-result-prefixes="msxml umbraco.library">

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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <xsl:for-each select="$currentPage/node [string(data [@alias='newsTeaser']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">
    <xsl:sort select="current()/data[@alias='newsTeaser']"  order="descending" />
           
    <xsl:if test="position() &lt;= 1">
    <h3>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a>
    </h3>
    <p><xsl:value-of select="./data [@alias = 'newsTeaser']"  disable-output-escaping="yes" /></p>
         
    </xsl:if>
    </xsl:for-each>

    </xsl:template>
    </xsl:stylesheet>
  • Garry Bain 149 posts 124 karma points
    Oct 26, 2009 @ 17:54
    Garry Bain
    0

    Oooh think I've found it, it was this line:

    <xsl:for-each select="$currentPage/node [string(data [@alias='newsTeaser']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">

    Needed to be changed to:

    <xsl:for-each select="$currentPage/node/node [string(data [@alias='newsTeaser']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">

    So the extra /node/ would work its way down to this item. Now - if I want to further extend this so that I can show this news item on every page - how would you configure the code so it specifically searches the news section - without needing to specify the depth using "$currentPage/node" - as this level will vary depending where you are on the site.

    Thanks again, Garry.

  • dandrayne 1138 posts 2262 karma points
    Oct 26, 2009 @ 17:59
    dandrayne
    0
    <xsl:for-each select="$currentPage/ancestor-or-self::root//node [string(data [@alias='newsTeaser']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">

       
    <xsl:value-of select="current()/@nodeName" />
    </xsl:for-each>

    Should find all news items throughout the site, no matter where you are.

    Dan

  • Garry Bain 149 posts 124 karma points
    Oct 27, 2009 @ 16:52
    Garry Bain
    0

    Excellent, thanks very much everyone, especially Dan.

    Garry.

  • sdfsfm 70 posts 94 karma points
    Aug 02, 2010 @ 20:46
    sdfsfm
    0

    Hi I found this post but am having trouble displaying news items on the homepage, Its currenlty blank, my xslt looks like this (NewsItem is the doc type name)

        <?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"
            exclude-result-prefixes="msxml umbraco.library">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
    <xsl:variable name="datenow" select="umbraco.library:CurrentDate()" />
    <!--<xsl:variable name="NewsItem" select="umbraco.library:GetXmlNodeById(/macro/NewsItem)" />-->

    <!--<xsl:for-each select="$NewsItem//node [string(data [@alias='newsDate']) != '' and string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='NewsItem' ]">-->
      <xsl:for-each select="$currentPage/ancestor-or-self::root//node [string(./newsDate) != '' and string(./umbracoNaviHide) != '1' and @nodeTypeAlias='NewsItem' ]">
      <xsl:sort select="current()/newsDate"  order="descending" />
            
    <xsl:if test="position() &lt;= 1">
    <h2><a title="View full story of {@nodeName}" href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="./newsHeading"/></a></h2>
    <p class="date"><xsl:value-of select="newsDate"/></p>
    <p><xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(./newsBodyText), 100,'...')"/></p>
    <p class="bg-greenBtn"><a title="Continue reading {@nodeName}" href="{umbraco.library:NiceUrl(@id)}"><span>Continue reading</span></a></p>
    </xsl:if>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

     

    I know this is an old post, but any help will be much appreciated

Please Sign in or register to post replies

Write your reply to:

Draft