Copied to clipboard

Flag this post as spam?

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


  • Matei Mihai 4 posts 24 karma points
    Jul 26, 2011 @ 12:34
    Matei Mihai
    0

    Loop to extract only top few pages

    Good day,

    I am using Umbraco V4.7. I have a macro that loops through some content (NewsArticle), extracting a property called "newsArticleContent".

    This works 100% and all articles display but what i'm actually working on at the moment is to have a summary of the top 3 articles in the order of which they were created. So if i have Article 1, Article 2, Article 3, Article 4 and Article 5, i only want to show Article 5, Article 4 and Article 3, since they are the latest ones created.

    This is the xslt i have at the moment:

    <xsl:param name="currentPage"/>
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    <xsl:template match="/">
      <xsl:for-each select="$siteRoot/NewsSection/NewsArticle[not(umbracoNaviHide = 1)]">
        <xsl:value-of select="newsArticleContent" disable-output-escaping="yes" />
      </xsl:for-each>
    </xsl:template>

    What i'm struggling with is sorting them and having a counter or index inside so that it only goes through the top 3. XSLT and macros are quite a challenge for me at the moment.

    What i also need to do is display only the first 20 words of the article's rich text editor property newsArticleContent, with a link at the end "[read more]" pointing the user to the actual article, but if i can just get to extract the top 3, i would be greatful.

    Any help would be highly appreciated.

    Thank you.

    Regards,
    Matei

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 26, 2011 @ 12:59
    Fuji Kusaka
    2

    Hi Matei,

    What you could do is in your docType add a field ArticlePublished and choose DatePicker.

      <xsl:param name="currentPage"/>
          <xsl:variable name="itemsToDisplay" select="3"/>
          
          <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
          <xsl:template match="/">
            <xsl:for-each select="$siteRoot/NewsSection/NewsArticle[not(umbracoNaviHide = 1)]">           
              <xsl:sort select="current()/ArticlePublished" order="descending"/>
               <xsl:if test="position alt; = $itemsToDisplay ">
                   <!-- only the first 20 words -->
                <xsl:value-of select="umbraco.library:TruncateString(newsArticleContent, 20, '...')" disable-output-escaping="yes"/><br/><a href="{umbraco.library:NiceUrl(@id)}">Read more </a>   

              </xsl:if>

            
            
            </xsl:for-each>
          </xsl:template>

    I hope this helps

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 26, 2011 @ 12:59
    Dennis Aaen
    1

    Hi Matei,

    I will try to help you the best I can. I have not tried it myself, but hope it can help you further.

    Maybe you could try something like

    <xsl:param name="currentPage"/>
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    <xsl:template match="/">
      <xsl:for-each select="$siteRoot/NewsSection/NewsArticle[not(umbracoNaviHide = 1)]">
        <xsl:sort select="@createDate" order="descending" />
           <xsl:if test="position() &lt;= 3">
            <xsl:value-of select="newsArticleContent" disable-output-escaping="yes" />
          </xsl:if>
      </xsl:for-each>
    </xsl:template>

    I have found inspiration for my reply through this post, so maybe this can help you.

    http://our.umbraco.org/forum/developers/xslt/5815-Sorting-output-by-date

    Hope at least this may help to help you solve your issues

    /Dennis

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 26, 2011 @ 13:18
    Fuji Kusaka
    0

    Opps....Slight error here

     

    Change
    <
    xsl:if test="position alt; = $itemsToDisplay ">

    to

    <xsl:if test="position() alt; = $itemsToDisplay ">
  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jul 26, 2011 @ 13:24
    Michael Latouche
    0

    I guess you meant

    <xsl:if test="position() &lt; = $itemsToDisplay ">

    :-)

    Cheers,

    Michael

  • Matei Mihai 4 posts 24 karma points
    Jul 26, 2011 @ 13:26
    Matei Mihai
    0

    Hey guys,

    This is great, amazing in fact, it works like a charm! Both of you helped me a lot. I don't suppose i can mark 2 answers as the solution... I'll mark Fuji's answer as right for showing me how to use the TruncateString function as well, it helps with the summary of the news.

    Thank you both!

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 26, 2011 @ 13:35
    Fuji Kusaka
    0

    Hi Matei glad if you got it working.

     

    //fuji

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jul 26, 2011 @ 13:51
    Dennis Aaen
    0

    Hi Matei,

    That´s alright with me :-). Glad that I could help you in some way.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft