Copied to clipboard

Flag this post as spam?

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


  • Alexandru 112 posts 272 karma points
    Oct 15, 2013 @ 10:57
    Alexandru
    0

    Filter posts within last month

    Hello,

    I have the following Macro:

    <xsl:template match="/">
    
      <xsl:for-each select="$currentPage/ancestor-or-self::umbBlog//umbBlogPost">            
        <xsl:sort select="count(BlogLibrary:GetCommentsForPost(@id)//comment)" data-type="number" order="descending" />       
          <xsl:if test="position()&lt; 5">
            <xsl:variable name="text" select="umbraco.library:StripHtml($currentPage/@nodeName)" />            
                <a href="{umbraco.library:NiceUrl(@id)}" class="popularPostsClass"><xsl:value-of select="./@nodeName" disable-output-escaping="yes"/></a>
                <br/>
          </xsl:if>   
      </xsl:for-each>
    

    I would like to apply what this macro does only to the posts within the last month. Is there any way I can surround this whole part with another sort that only reads the posts within the last month?

    Thank you!

  • Dan 1288 posts 3921 karma points c-trib
    Oct 15, 2013 @ 20:22
    Dan
    101

    Hi Alexandru,

    You should be able to use a predicate on your node selector to select only those articles within a certain range from today. Umbraco has some XSLT extension methods available to help with this, namely: FormatDateTime, CurrentDate and DateDiff. It should allow you to do something like this (not tested!):

    <!-- Get current date -->
    <xsl:variable name="currentDate" select="substring-before(umbraco.library:CurrentDate(), 'T')"/>
    
    <!-- Add a predicate to node selection to get articles where the difference between blogPostDate property
     and currentDate variable is less than 43829 minutes(! which equates roughly to 1 month)  -->
    <xsl:for-each select="$currentPage/ancestor-or-self::umbBlog//umbBlogPost[umbraco.library:DateDiff(umbraco.library:FormatDateTime(blogPostDate,'yyyy-MM-dd'), $currentDate, 'm') < 43829]">            
        <xsl:sort select="count(BlogLibrary:GetCommentsForPost(@id)//comment)" data-type="number" order="descending" />       
        <xsl:if test="position()< 5">
            <xsl:variable name="text" select="umbraco.library:StripHtml($currentPage/@nodeName)" />            
            <a href="{umbraco.library:NiceUrl(@id)}" class="popularPostsClass"><xsl:value-of select="./@nodeName" disable-output-escaping="yes"/></a>
            <br/>
        </xsl:if>   
    </xsl:for-each>
    

    Hopefully this gets you started...

  • Alexandru 112 posts 272 karma points
    Oct 16, 2013 @ 09:36
    Alexandru
    0

    Hi Dan,

    For now I am using this:

    <xsl:for-each select="$currentPage/ancestor-or-self::umbBlog//umbBlogPost">          
            <xsl:sort select="count(BlogLibrary:GetCommentsForPost(@id)//comment)" data-type="number" order="descending" />   
                    <xsl:variable name="currentDate" select="umbraco.library:FormatDateTime(umbraco.library:CurrentDate(), 'dd-MM-yyyy')"/>
                    <xsl:variable name="postDate" select="umbraco.library:FormatDateTime(umbraco.library:LongDate(./PostDate), 'dd-MM-yyyy')"/>
                    <xsl:variable name="diffSeconds" select="umbraco.library:DateDiff($currentDate, $postDate, 's')"/>
                    <xsl:variable name="diffDays" select="floor($diffSeconds div (60 * 60 * 24))"/>
    
                    <xsl:choose>
                        <xsl:when test="$diffDays &lt; 31">
                            <li>
                                <a href="{umbraco.library:NiceUrl(@id)}" class="popularPostsClass"><xsl:value-of select="./@nodeName" disable-output-escaping="yes"/></a>
                                <br/>
                            </li>
                        </xsl:when>
                        <xsl:otherwise>
                        </xsl:otherwise>
                    </xsl:choose>
        </xsl:for-each> 
    

    and I hide the rest that I don't need through CSS. but I will give your version a try as I would rather avoid having a huge list hidden by CSS. Thanks, I will get back to you with how it goes!

  • Alexandru 112 posts 272 karma points
    Oct 16, 2013 @ 09:58
    Alexandru
    0

    It is errorless but it seems the predicate won't work. Regardless of the number of minutes I put in, it still reads older posts.

  • Dan 1288 posts 3921 karma points c-trib
    Oct 16, 2013 @ 10:12
    Dan
    0

    Hi Alexandru,

    There are quite a lot of things that can be done to help in debugging this, for example, try putting this inside your loop and see what it returns:

    <xsl:value-of select="umbraco.library:DateDiff(umbraco.library:FormatDateTime(PostDate,'yyyy-MM-dd'), $currentDate, 'm')" />
    
  • Alexandru 112 posts 272 karma points
    Oct 16, 2013 @ 10:15
    Alexandru
    0

    It seems I am getting negative values. Let me try to fix that and see if there's any change.

  • Alexandru 112 posts 272 karma points
    Oct 16, 2013 @ 10:19
    Alexandru
    0

    Aye, fixed it. It should be '> [negative value of timespan]'

    you should change this in your post so that others can have the right version too :)

    It's this:

    <xsl:for-each select="$currentPage/ancestor-or-self::umbBlog//umbBlogPost[umbraco.library:DateDiff(umbraco.library:FormatDateTime(umbraco.library:LongDate(./PostDate), 'dd-MM-yyyy'), $currentDate, 'm') &gt; -43829]">            
    
  • Dan 1288 posts 3921 karma points c-trib
    Oct 16, 2013 @ 10:19
    Dan
    0

    Ah okay, try this as the predicate in that instance:

    [umbraco.library:DateDiff($currentDate, umbraco.library:FormatDateTime(PostDate,'yyyy-MM-dd'), 'm') &lt; 43829]
    
  • Alexandru 112 posts 272 karma points
    Oct 16, 2013 @ 10:32
    Alexandru
    0

    Thanks for the support!

Please Sign in or register to post replies

Write your reply to:

Draft