24

You need to add .first() when setting the category. As far as I can tell, if you don't have .first() only {{ category.slug }} will work. First get the slug: {% set catSlug = craft.request.getSegment(3) %} This will give you the CategoryModel object for catSlug: {% set category = craft.categories.slug(catSlug).first() %} You can now access whatever you ...


5

That’s definitely the way to do it! I would set the URL segments to their own variables, to clean it up a little bit. {% set entrySlug = craft.request.getSegment(2) %} {% set imageOffset = craft.request.getSegment(4) - 1 %} Also, probably a good idea to include the section param when fetching the entry, in case you have another entry in another section ...


4

You might do something like this, concerned that you want to set current on active category. {% set categorySlug = craft.request.getLastSegment() %} {% nav category in craft.categories.group('blogCategories') %} {% set current = (category.slug == categorySlug) %} <a class="button button--outline-blue {% if current %} current{% endif %} ...


2

This is just a guess, as I've never used the 'contains' method before, but it's possible that it can't be used directly with an ElementCriteriaModel query. You might have to filter the entries after the fact. {% set entries = craft.entries.section('news') %} {% for entry in entries if entry.category.contains(craft.request.getSegment(2)) %} {{ entry....


2

Got it: Had already set the slug variable to the first segment in _layout.twig thus overriding everything after. m(


2

Code above is fine. I was stupidly updating the wrong template


2

To check if the word jobs is in the sectionUrl you can use twig's matches to create a regular expression that looks for jobs {% set allSections = craft.sections.getAllSections() %} {% for section in allSections %} {% set sectionURL = section.getUrlFormat()|split('/')[0] %} {% if sectionURL matches "/jobs/" %} {{ section.name }} {% endif ...


2

You should be able to utilize twig's built-in containment operator. Your code would look like this: {% if 'vascular' in craft.request.getSegment(3) %} ... {% endif %} Here is official documentation on the containment operator: http://twig.sensiolabs.org/doc/templates.html#containment-operator.


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.


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

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

If I understand you right all you need is: {% for section in allSections if craft.request.getFirstSegment() == 'jobs' %} {# other code here #} {% endfor %} You can give conditions to your for loop which saves you another if statement. You also dont need to split any url because craft already got Segment selection which is found here: https://craftcms....


1

That's because getSegments() returns an array. You can either get one of those segments by using: {% set segments = craft.request.getSegments() %} {{ segments[0] }} Or loop through all returned segments: {% for segment in craft.request.getSegments() %} {{ segment }} {% endfor %}


1

I successfully managed to set this up with Douglas' first solution above combined with another answer also from Douglas about array merging. Without this merge I had to repeat all my template code first in a 'for' loop without a request segment and then again inside a 'for' loop with a request segment. By adding the merge I ended up with this working ...


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