We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36782
    • 109 Posts
    HI
    I'm attempting to do the following w/ a snippet:

    Test if the TV "showInfo" is 1 (as opposed to 0 )
    If it is, load the 'meta info' chunk, and load in the user who posted it, and the date it was posted.

    My code looks like so:
    $output = array();
    $resource = $modx->getObject('modResource',$id);
    $output['publisher'] = $resource->get('createdby');
    $output['publishDate'] = date("F j, Y",$resource->get('publishedon'));
    $show = $resource->getTVValue('showInfo');
    if($show){
            return $modx->getChunk('Meta.tpl',$output);
    };
    

    And the template chunk:
    <p class="meta">
    Posted by [[+poster]] on [[+published]]
    </p>

    I can't quite figure out what I've done wrong... this is a revised edition after I could only get epoch. Now I get nada.
    Can anyone point me in the right direction?
    Thnx [ed. note: unsub777 last edited this post 14 years, 11 months ago.]
      • 3749
      • 24,544 Posts
      First of all, you're setting "publisher" and "publishDate" and in your Tpl chunk, you're displaying "poster" and "published," which don't exist.

      Second, createdby will give you a user ID, which is probably not what you want to display. wink


      Try something like this (untested):

      <?php
      $output = array();
      $resource = $modx->getObject('modResource',$id);
      
      if ($resource) {
          $publisher  = $resource->getOne('CreatedBy');
      } else {
          return 'Resource not found';
      }
      
      if ($publisher) {
          $output['publisher'] = $publisher->get('username');
      } else {
             $output['publisher'] = 'anonymous';
      }
      
      $output['publishDate'] = $resource->get('publishedon'));
      $show = $resource->getTVValue('showInfo');
      if($show){
              return $modx->getChunk('Meta.tpl',$output);
      } else {
          return "";
      }
      

      Then, in the Tpl chunk:

      <p class="meta">
      Posted by [[+publisher]] on [[+published:strtotime:date=`%m-%d-%Y`]]
      </p>


      BTW, if this is for the current resource, you can skip the getObject() call and replace it with this:

      <?php
      $resource =& $modx->resource;


      In that case, you also won't need the "if" test for $resource.

      [ed. note: BobRay last edited this post 14 years, 11 months ago.]
        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
        • 36782
        • 109 Posts
        Hi,
        Thank you so much for this, it's actually helped me figure out a couple different things besides smiley