URL Segment Variables
ExpressionEngine provides an easy way for you to access the information in your URL segments. By "segments", we mean the segments of the URL that appear after your index.php page. For example, this URL has two segments:
http://www.example.com/index.php/products/shirts/
products is Segment 1 and shirts is segment 2
If needed, you could access the values of either of those segments using these variables in your templates:
{segment_1} or {segment_2}.
Up to 9 URL segments can be accessed using the following variables:
{segment_1}
{segment_2}
{segment_3}
{segment_4}
{segment_5}
{segment_6}
{segment_7}
{segment_8}
{segment_9}
Segment variables let you dynamically change aspects of your templates based on what appears in the URL.
For example, imagine you use an ExpressionEngine weblog to store information about each employee in your company. Each weblog entry describes a different person, with the URL Title of the entry being the person's name. In this scenario you could use a single template to dynamically show each employee's information based on what is in the URL. Consider this URL:
http://www.example.com/index.php/company/employees/joe/
The Template Groups name is "company" and the Template name is "employees". Based on the information in the 3rd segment (in this case, "joe") you can dynamically cause the "employees" template to change for each person. Here's an example of a weblog tag in which the URL title changes based on the 3rd segment:
{exp:weblog:entries url_title="{segment_3}"}
<h1>{title}</h1>
<p>{body}</p>
{/exp:weblog:entries}
User Contributed Notes
Note also that segment variables can be combined with native EE conditional variables to control content display in a template, e.g.:
{if segment_2 == ''}... content ...
{if:else}
... other content ...
{/if}
This is useful when you want, say, and index page page of a weblog (with 10 latest entries as summaries) to display from the same URL as the full text of the entry itself. That is, ‘example.com/blog/’ is the front page whereas ‘example.com/blog/entry-title/’ displays a single post from the same template.
(cf. Conditional Variables)
My favorite tip is that you can access segments earlier via PHP, which means you can write different weblog:entries tags in one template depending on URL context:
<?
global $IN;
$seg1 = $IN->fetch_uri_segment('1'); // these are interchangeable
$seg2 = $IN->SEGS[2]; // these are interchangeable
if ($seg2 != '' && $seg1 == 'foo') { // limit sub pages to 25 items
?>
{exp:weblog:entries limit="25"}
<? } else { // but the main page shows only 5
?>
{exp:weblog:entries limit="5"}
<? } ?>
(Thanks, Derek Derek Derek for the tutorial.)
You must have an ExpressionEngine license and have attained a forum rank of "Lab Assistant" (100 posts) to contribute notes to the User Guide