We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45078
    • 34 Posts
    OK, I've spent way too long trying to find the answer to this and feel like I've read every bit of documentation and forum thread on the topic.

    I have Articles installed and I'm using tagLister's tolinks snippet with useTagsFurl=`1` to display the associated tags on an Article page (not an overall tag cloud). This is all working and displaying fine, my tag URLs are formatted like website.com/tags/tag+name.

    My problem is, I can't work out how to display the results (using getResourcesTag). I have created a resource to display the results called 'tags', which contains the getResourcesTag snippet as per the documentation - however it results in a 404 error, obviously due to the friendly url formatting. Has anyone done this successfully, or can see what I'm doing wrong?

    This question has been answered by rodoneill. See the first response.

    • discuss.answer
      • 45078
      • 34 Posts
      Instead of trying to decipher the scattered documentation and examples, I've put my own solution together that works just how I want.

      Again, I'm using 'tolinks' snippet to generate article tag links in the following format 'website.com/tag/tag-name' with '/tag/' being a resource that has been designed to display results.

      I created the following snippet 'GetTaggedArticles':

      <?php
      if(isset($_GET['tag'])) {
          $tag = strip_tags($_GET['tag']);
          $tag = str_replace('-', ' ', $tag);
          
          // get all resources with tag value in their 'articlestags' TV
          $articlesContainerId = 3;
      
          $params = array(
              'parents' => $articlesContainerId,
              'tpl' => 'myArticleListingTemplate',
              'showHidden' => 1,
              'limit' => 0,
              'includeTVs' => 1,
              'processTVs' => 1,
              'hideContainers' => 1,
              'tvFilters' => 'articlestags==%' . $tag . '%'
          );
          $output = $modx->runSnippet('getResources', $params);
           
          return $output;
      }
      


      This gets the value from the query string ?tag, then I run the 'getResources' snippet to return all resources that contain this value as part of the 'articlestags' TV.

      To make this snippet work and see the ?tag query string, I've added a rewrite condition (making sure to place it before the friendly url rewrite - RewriteRule ^(.*)$ index.php?q=$1 [L,QSA,NC]) in my .htaccess file:

      <IfModule mod_rewrite.c>
          
          # rewrite /tag/tag-name to /tag?tag=tag-name 
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          
          RewriteRule ^tag/([\w'-]+)$ /tag?tag=$1 [L,QSA]
      
      </IfModule>
      

      This rule will rewrite /tag/tag-name to /tag?tag=tag-name using a regular expression and QSA (Query String Append).


      Then, I simply call the snippet on the results resource to display my tagged articles:
      [[!GetTaggedArticles]]


      There is probably a much faster way to do this and I'd appreciate any input or suggestions, but for now this has got me out of a jam.

      Hopefully others will find this helpful.