We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37859
    • 34 Posts
    Hello,

    I'm using getResources to display an overview of newsitems. Works fine.

    What I want to achieve is to display a label "New" besides the pagetitle (newsitemtitle) in my getResources overview for each newsitem that is published the last 2 weeks.
    Therefore I'm using a snippet I found in Bob's Blog : http://bobsguides.com/blog.html/2015/03/09/modx-snippet-development-iii/

    This works on the page with the full newsarticle (where I call the snippet in the template), but not on the page with the getResources call results. Here I call the snippet in the tplChunk of the getResources call.

    On the page with the getResources call, the snippet takes the publishedon date of the page withe the getResources result instead of the publishedon date of each retrieved resource with a newsitem.

    Any idea how to make this work ?

    This is Bob's snippet :
    /* NewResource snippet */
    /* Default return value */
    $output = '';
    /* Return value for new resources that are published */
    $newOutput = '<span class="label">NEW!</span>';
    /* Set $interval as the number of seconds in two weeks */
    $interval = 1209600;
    /* See if the resource is published */
    $published = $modx->resource->get('published');
    /* For published documents, see if publication date is
       within the last two weeks */
    if ($published) {
        /* Get the current time as a timestamp */
        $currentTime = time();
        /* Get the publishedon date (if any) and
           convert it to a unix timestamp */
        $ptime = strtotime($modx->resource->get('publishedon'));
        /* Get the resource's age in seconds */
        $age = $currentTime - $ptime;
        if ($age < $interval) {
            /* Yes, it's recent - return NEW! */
            $output = $newOutput;
        }
    }
    return $output;
      • 4172
      • 5,888 Posts
      you can extend the snippet a bit

      <?php
      
      /* NewResource snippet */
      /* Default return value */
      $output = '';
      /* Return value for new resources that are published */
      $newOutput = '<span class="label">NEW!</span>';
      /* Set $interval as the number of seconds in two weeks */
      $interval = 1209600;
      /* See if the resource is published */
      $published = $modx->getOption('published',$scriptProperties,$modx->resource->get('published'));
      $publishedon = $modx->getOption('publishedon',$scriptProperties,$modx->resource->get('publishedon'));
      /* For published documents, see if publication date is
         within the last two weeks */
      if ($published) {
          /* Get the current time as a timestamp */
          $currentTime = time();
          /* Get the publishedon date (if any) and
             convert it to a unix timestamp */
          $ptime = strtotime($publishedon);
          /* Get the resource's age in seconds */
          $age = $currentTime - $ptime;
          if ($age < $interval) {
              /* Yes, it's recent - return NEW! */
              $output = $newOutput;
          }
      }
      return $output;
      


      and call it with
      [[NewResource? &published=`[[+published]]` &publishedon=`[[+publishedon]]`]]
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 37859
        • 34 Posts
        You made my day Bruno17 !
        This did it.
        I notice I still have to learn a lot about modx.
        Thanks !
          • 3749
          • 24,544 Posts
          Nice. I updated the blog post. Are you sure the NewResource tag should be cached?
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting