10

Craft 2: craft.request.getSegment(2), see Craft docs Craft 3: craft.app.request.getSegment(2)


6

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 like so: return [ '<country_code:[a-z]{2}(-[a-z]{2})?>/<section_URI:team>' => ['template' => 'team'] ]; And then in your twig template you ...


4

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 conditional in your template to something like this: {% if craft.request. getParam('receipt') == 'thanks' %} ...thank you message {% endif %} I've always felt ...


4

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 set the variable w/ a ternary operator to make it a one-liner: {% set assetField = craft.request.getSegment(1) == 'special' ? entry.assetFieldSpecial : entry....


4

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 in assets %} ... {% endfor %}


4

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


4

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+>?' => ['template' => '_extract'], This will then populate the _extract template with the twig variable {{ slug }} and also the {{ option }} variable, if ...


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/<regionId:[^\/]+>' => ['template' => 'pricing/_index'], ]; By defining the named subpattern, the template will get loaded with a regionId variable ...


3

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/index.php, @web will already resolve to http://website.test/en. The alias @web/en you are using for your site’s base URL does therefore resolve to http://website....


3

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 site structure. And even then, you might need to make a lot of requests to retrieve the data. You would likely need to do a combination of static logic, looking ...


3

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.


3

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(craft.request.getSegment(3)).relatedTo(category) %} {% if not entries %} {% exit 404 %} {% endif %}


3

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 ?m=thankyou so it appends to the current url and retains the previous segments. In your form: <input type="hidden" name="redirect" value="?m=thankyou"> ...


2

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 have category defined in the template without needing: {% set categoryGroup = craft.categories.group('Sections') .level(1) .slug(craft.request....


2

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 handle the entire routing on your own. Channels If it's for a channel with a set URI format like foo/{slug} you can implement a route like Event::on( ...


2

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


2

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-directories from a single domain name, the first segment of the URL is considered to be part of the siteUrl and will not be returned in the segments array.


2

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 for the first segment, you can try (craft.app.request.segments|first == 'uk') to get that information.


2

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(-2) }} will get the next to last segment


2

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.section('services').slug(entrySlug).one() %} {% if entry is not defined or entry is null %} {% exit 404 %} {% endit %} And then I've got two routes: services/...


1

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 entry is related to the entry you want to fetch this would be the besser solution. However that URL structure is not really optimal to beginn with. There are ...


1

{% 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() returns an array of segments in line 1: check if there is a second to last item slice(-2, 1): -2 target second to last item and you only that one so second ...


1

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}}


1

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)


1

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.relationFieldHandle will contain an ElementCriteriaModel with a ready to use query to fetch the related elements. So entry.relationFieldHandle.first() will ...


1

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, you could use getSegments() and conditionally grab a part depending on count of segments returned. {% set segments = craft.request.getSegments() %} {% set ...


1

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 strip all / and 2 from the beginning and end of the given string, which explains the output you're seeing. I think you'll be better off by installing something ...


1

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.request.path }} grabs the remaining portion of the url, after the region segment! So, I can use code that I've written above and I only need to add: <!-- language:...


1

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 = craft.request.getSegment(2) %} {% set seg3 = craft.request.getSegment(3) %} So maybe something like this? The keyword in is Twig's way of checking in_array() ...


1

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 example as to what the URL is for a specific entry compared to what you need it to be?


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