We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3232
    • 380 Posts
    Is it possible with Ditto (or any other snippet) to display 3 related items?

    I have a tv that is used for tags, it would be nice to display other documents with similar tags in the right column on one of my pages.

    I have been looking at http://ditto.modxcms.com/files/assets/snippets/ditto/extenders/tagging-extender-inc-php.html and can’t seem to get it to work the way I want it to.

    Is there any way to display related content? can it select one tag at random and display related items that also have that tag?

    Any help or direction would be much appreciated.

    Thanks
    Brian




      • 3232
      • 380 Posts
      For anyone else looking to do this, I created a new snippet that extracted one of the tags at random and then I used ditto to take that tag and filter against it.
        • 1914
        • 6 Posts
        sounds like what i am looking for wink can you post the snippet here?
          • 3232
          • 380 Posts
          If you have any questions, let me know.

          <?php
          /*
          --------------------------------------------------
          randomTag
          [[randomTag? &docid=`` &tvtag=`` &delimiter=``]]
          --------------------------------------------------
          Parameters
          --------------------------------------------------
          */
          
          # $docid [ int ]
          # ID of the document for which to get a template variable content.
          # Default: current document
          
          $rtIntDocId = (isset($docId)) ? $docid : $modx->documentIdentifier;
          
          # $tvtag [ string ]
          # Name of the field where the tags are located.
          # Default: 'null'
          
          $rtStrDocField = (isset($tvtag)) ? trim($tvtag) : 'null';
          
          # $delimiter [ string ]
          # The delimiter that seporates the tags in the template variable.
          # Default: ", "
          
          $rtBoolDelimiter = (isset($delimiter)) ? $delimiter : ', ';
          
          /* Do not edit the code below!
          ----------------------------------------------- */
          # get content of the field
          $output = $modx->getTemplateVarOutput($rtStrDocField,$rtIntDocId);
          $field = $output[$rtStrDocField];
          
          # break out tags from field
          $rtSplit = split($rtBoolDelimiter, $field);
          
          # count number of tags
          $rtSplitNumber = count($rtSplit);
          
          # select random number and associate array
          $rtNum = rand(1, $rtSplitNumber);
          $rtNum = $rtNum - 1;
          
          # clear it
          unset($rtIntDocId, $rtStrDocField, $rtBoolDelimiter);
          
          return $rtSplit[$rtNum];
          ?>
            • 1914
            • 6 Posts
            Thank you very much laugh
              • 29646
              • 17 Posts
              This is great! Just what I’ve been looking for. Thank you very much!

              -aj
              ---
              A.J. Van Beest
              Beest Built Websites, LLC
              (877) 292-3378
                ---
                A.J. Van Beest
                Beest Built Websites, LLC
                (877) 292-3378
                • 29646
                • 17 Posts
                OK, I have a basic question about using this in my page: Um, how do I use this in my page? Obviously, I’m working my up the learning curve.  smiley

                Here’s my Ditto call:
                [!Ditto? &filter=`tvpage_tags,[[randomTag]],2` &display=`3` &tpl=`ditto_related_items` !]
                but this isn’t allowing [[randomTag]] to run (so it seems). I tried running [[randomTag]] before the Ditto call, but that just put the tag name on the page. How should I pass that value to the Ditto call?

                Thanks for your help!

                -aj
                ---
                A.J. Van Beest
                Beest Built Websites, LLC
                (877) 292-3378
                  ---
                  A.J. Van Beest
                  Beest Built Websites, LLC
                  (877) 292-3378
                  • 3232
                  • 380 Posts
                  Quote from: theaj42 at Jan 21, 2009, 11:45 AM

                  OK, I have a basic question about using this in my page: Um, how do I use this in my page? Obviously, I’m working my up the learning curve. smiley

                  Here’s my Ditto call:
                  [!Ditto? &filter=`tvpage_tags,[[randomTag]],2` &display=`3` &tpl=`ditto_related_items` !]
                  but this isn’t allowing [[randomTag]] to run (so it seems). I tried running [[randomTag]] before the Ditto call, but that just put the tag name on the page. How should I pass that value to the Ditto call?

                  Your Ditto call should look something like this:

                  [!Ditto? &filter=`[[randomTag? &tvtag=`vpage_tags`]],2` &display=`3` &tpl=`ditto_related_items` !]
                  


                  This is assuming that the current id/page has a TV named vpage_tags

                  If you still need help, let me know.