We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12410
    • 353 Posts
    Wow - thanks guys! Here’s what I did so and it works fine.

    I created the snippet getUrlSegments

    I added lucas’s code
    <?php
    $output = '';
    
      $url  = $_SERVER['REQUEST_URI'];
      $path = parse_url($url);
      
      $segments = explode('/', $path['path']);
     
      foreach ($segments as $s => $segment) {
        if ($s > 0) {
          $output .= $modx->setPlaceholder('segment'.$s, $segment);
        }
        if ($s == count($segments) - 1) {
          $output .= $modx->setPlaceholder('segmentLast', $segment);
        }
      }          
         
    return $output;


    and in my template

    I added eg:

    <html>
    <body>
    [[!getUrlSegments]]
    [[!+segment1]]


    </body>
    </html>

    ..these all worked too:
    [[!+segment1]]
    [[!+segment2]]
    [[!+segment3]]
    [[!+segmentLast]]


    Mark H and Lucas may advise if this aint the recommended way.

    THANKS AGAIN GUYS!


      • 12410
      • 353 Posts
      One quick q - I noticed my last friendly url segment in modx revo ends in alsation.php
      so www.mysite.com/animals/dogs/alsation.php
      Can I lose the .php at the end somehow in the freindly url settings?
      Thanks
        • 33968
        • 863 Posts
        Yeah man that should be fine. Still unsure about caching but will get to the bottom of this.

        Also, if you only want to grab one segment it might be more efficient to use the first snippet I suggested above. You could probably cache that one too. Probably tongue

        To lose the .php extension, maybe have a look under System > Content types
          • 12410
          • 353 Posts
          Cool combined with the IF extra this becomes really useful!
          http://rtfm.modx.com/display/ADDON/If


          [[!If? 
          &subject=`[[!urlSegment? &segment=`3`]]`
             &operator=`EQ`
             &operand=`section-3a`
             &then=`code to show when true`
             &else=`code to show when false`
          ]]



          cached seems to work fine too.
          [[!If? 
          &subject=`[[urlSegment? &segment=`3`]]`
             &operator=`EQ`
             &operand=`section-3a`
             &then=`code to show when true`
             &else=`code to show when false`
          ]]

            • 33968
            • 863 Posts
            Despite all the above (and I guess urlSegment would come in handy for some applications), I would still encourage you to go with this:
            [[UltimateParent:is=`X`:then=`code to show when true`:else=`code when false`? &topLevel=`2`]]
            

            That keeps everything modx-friendly and will continue to work even if your end user figures out how to change the ’segment’ name smiley
              • 12410
              • 353 Posts
              Just dawned on me that i can achieve similar by doing the below with the if extra. God I'm slow smiley

              [[!If?
              &subject=`[[*id]]`
                 &operator=`==`
                 &operand=`7`
                 &then=`my code`]]