Yuh, do ExpressionEngine urls really look like that? What’s with the index.php as a folder:
http://example.com/index.php/products/shirts/
As my Saturday morning present to you, I’ve coded up ’urlSegment’
Call it like this:
[[!urlSegment? &segment=`X`]]
Where ’X’ is the position of the segment you want to grab. Defaults to the first segment.
eg:
http://www.example.com/animals/dogs/alsatian
To grab ’dogs’: [[!getSegment? &segment=`2`]]
To grab ’alsatian: [[!getSegment? &segment=`3`]]
Simple stuff.
Snippet code:
<?php
$output = '';
$segment = $modx->getOption('segment',$scriptProperties,'1');
$url = $_SERVER['REQUEST_URI'];
$path = parse_url($url);
$segments = explode('/', $path['path']);
if ($segments[$segment]) {
$output = $segments[$segment];
} else {
$output = 'Segment ' . $segment . ' does not exist!';
}
return $output;
This is just an example but if it’s the code could easily be extended to output each segment to a placeholder, something like [[+segment1]], [[+segment2]], [[+segmentLast]] etc.