Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
12 votes
Accepted

How to get a URL segment

Craft 2: craft.request.getSegment(2), see Craft docs Craft 3: craft.app.request.getSegment(2)
user avatar
  • 1,897
8 votes
Accepted

How to get second last url segment

You can use negative numbers as the parameter for segment which then counts from right to left. So {{ craft.app.request.segment(-1) }} will get the last segment. And... {{ craft.app.request.segment(-...
user avatar
  • 1,979
6 votes

How to get a URL segment

There's also another way that would be more consistent, in case your segments are not always in the same segments array position. You'd make your routes to match a certain mask in the routes.php file ...
user avatar
4 votes

How to use url segments like with Freebie in EE

One easy way to work around this is to use a query string instead of a segment. You'd redirect the user to for instance /contact?receipt=thanks instead of /contact/thanks, and you change the ...
user avatar
  • 7,228
4 votes

Setting a variable assets field, depending on the URL?

The issue with your original code is that you're looping through the "default" field, i.e. entry.assetfield, not the variable you set (assetfield). @carlcs' answer should do it, though you could also ...
user avatar
4 votes
Accepted

Setting a variable assets field, depending on the URL?

I'd probably do it like so: {% if craft.request.getSegment(1) == 'special' %} {% set assets = entry.assetfield_1 %} {% else %} {% set assets = entry.assetfield_2 %} {% endif %} {% for asset ...
user avatar
  • 35.8k
4 votes

Segment to filter through categories slug

Try removing the ' ' from segment in line 2 e.g. {% set pageCats = craft.categories('photoCategories').slug(segment) %}
user avatar
4 votes

Template logic to address a third URL segment

I would suggest you look into using Craft's Advanced Routing functionality to accomplish this. For instance, add the following to your config/routes.php: 'stories/<slug:\w+>/?<option:\w+>...
user avatar
3 votes
Accepted

Use url segment as querystring parameter [Craft 3]

You need to define routes for these URLs in settings in the CP or via the config/routes.php file. return [ 'team/<regionId:[^\/]+>' => ['template' => 'team/_index'], 'pricing/<...
user avatar
  • 35.8k
3 votes

Locale URL segment being duplicated, eg https://website.test/en/en/

Your issue is that the @web alias resolves to this: The URL to the folder that contains the index.php file that was loaded for the request Now that you’re serving your English site from web/en/...
user avatar
  • 35.8k
3 votes
Accepted

How to get an entry title and URI from a segment in order to create a breadcrumb navigation

If your site is mixture of singles, structures and channels then there is no easy way to get the entry titles and or uris from the segments alone. You would have to add some logic based on your known ...
user avatar
3 votes
Accepted

Override a wildcard dynamic route template

Sort order matters, move the more specific route up and you should be good. Alternativly set it up in routes.php using some regular expressions.
user avatar
  • 35.8k
3 votes
Accepted

check if a route segment matches a section entry type

Take a look at the {% exit %} tag. You should be able to do something very similar, since your search will return no results if the entry type does not exist. {% set entries = craft.entries.type(...
user avatar
3 votes
Accepted

AMForms Action URL

I am not a big fan of using segments for form submission pages. It's an oldschool EE habit and these are pages you don't want listed anyway so query away. Maybe just have your form success page set to ...
user avatar
  • 1,517
2 votes
Accepted

How can I get all child categories - of a child category based on url segment?

There are a couple of things I would do differently here. The first is to check the Categories in this group have their own URLs box in the Category Group settings. Set the url to {slug}, and you will ...
user avatar
2 votes
Accepted

Templates, Routes, and URL segments

Yes and no. Yes it is possible to include as many additional segments to selected routes and it's even possible to achieve what you want but it might become resource heavy and you would need to ...
user avatar
2 votes

How do I remove segment 1 in my URL AND get the parent URI?

Edit your section‘s settings (Settings > Sections > [Section Name]), and delete the static/ from the Entry URI Format.
user avatar
2 votes

Request Segment URL

Assuming you're using Craft's multi-site functionality(?), you'll want to use something like {{ currentSite.handle }} to check which site they're on. For multiple sites configured to use sub-...
user avatar
  • 3,829
2 votes

Request Segment URL

The request segments will come back as an array, where each part between slashes will be an array element so mysite.com/first/second/third gives segments [first,second,third] If you're just looking ...
user avatar
2 votes
Accepted

Routing: ignoring a segment from the URL

I did something similar for a site that showcased projects. In the top of my entry page, I had code like this: {% set entrySlug = craft.app.request.getSegment(2) %} {% set entry = craft.entries....
user avatar
  • 482
1 vote

Filter by slug from URL (advanced)

Why don't you use your current entry as reference? {% set nutritions = craft.entries.section('nutrition').relatedTo({ field: 'restaurant', targetElement: entry }).all() %} Since your current ...
user avatar
1 vote

How to get second last url segment

{% if craft.app.request.segments|length > 1 %} {% set secondToLast = craft.app.request.segments|slice(-2, 1)|first %} {% else %} no second to last {% endif %} {{ secondToLast }} getSegments() ...
user avatar
  • 467
1 vote

Routing and Segments

I never do this before, but maybe it works. return [ '<value:{slug}>-recipes' => ['template' => '_recipes/restaurant'], ] and you can get the value in a template like this : {{value}}...
user avatar
  • 437
1 vote
Accepted

Routing and Segments

Route: {slug}-recipes The in the template: {% set parts = craft.app.request.getSegment(1)|split("-") %} {% set newslug = parts[0] %} Then you can use: .slug(newslug)
user avatar
  • 451
1 vote

Categories Structure for Landing Pages

If you category is related to your entry there is no need to fetch the category via url segment. This method is slower and will need more resources since you'll do the work twice for no reason. entry....
user avatar
1 vote

Trimming lastSegment from a URI trims that PLUS the last number of the previous segment

Instead of doing complicated things with the result of getLastSegment(), can't you just get the 4th segment using getSegment(4)? If the segment you’re interested in is not always in the same position,...
user avatar
  • 35.8k
1 vote
Accepted

Trimming lastSegment from a URI trims that PLUS the last number of the previous segment

Twig's |trim filter is just a wrapper for PHP's trim() method, which says: Strip whitespace (or other characters) from the beginning and end of a string So in your example, you're telling it to ...
user avatar
  • 66.4k
1 vote
Accepted

Is it possible to change one segment of the current url and keep the rest of it the same in a cleaner way?

So!!!! I was getting confused about the various craft properties pertaining to urls, and I thought that even if I grab the path, that it would include the region segment, but surprise! {{ craft....
user avatar
1 vote
Accepted

How to write a segment that is not two other segments

I personally try to avoid inlining too many conditionals because I feel like it leaves room for logical loopholes. I also think using variables for segments keeps things easier to read. {% set seg2 = ...
user avatar
  • 1,887
1 vote

Custom routes using kebab {type} in URL segment

You could have a try with dynamic routing, where you map a URL (or a url pattern) to a specific template. Dynamic routes take priority over template paths. If I misunderstood you, can you give an ...
user avatar
  • 701

Only top scored, non community-wiki answers of a minimum length are eligible