3

Similar to this question: Output URL to section

Let's say we have a section with handle 'places' which defines its URL format as 'places/{slug}'—how do I get the link to the 'places' index page? The question above goes via the entries in that section but that just gets the url of the first entry in that section.

4 Answers 4

3

Here's what I did. You'll need to set sectionId first.

{% set thing = craft.sections.getSectionById(sectionId) %} {{thing.getUrlFormat()|split('/')[0]}}

1
  • 2
    In the newest version of Craft getSectionById query should be expanded with craft.app.sections.getSectionById(sectionId) Commented Nov 19, 2019 at 1:25
3

Expanding a little bit more on Dustin C solution.

It is possible to get the siteURL instead of hard coding it:

<a href="{{ siteUrl }}{{ entry.section.name|kebab }}">{{ entry.section }}</a>
2

I know this is a year old, but I was scratching around trying to find a solution to this, and I came up with this.

Try the kebab modifier.

<a href="http://example.com/{{ entry.section|kebab }}">{{ entry.section }}</a>

The kebab case makes it possible to convert your section titles to usable links -- at least they did in my case.

Hope this helps someone in the future.

1

Currently there is no method to get the link to the section's index page. However, if you try to visit http://domain.com/sectionhandle/, it will will try to load a template named index.html inside a folder named after your sectionhandle.

In your example:

http://domain.com/places/ -> templates/**places/index.html

I think that you can easily make that assumption and use that in a link. It's not elegant, and it doesn't take into account locales, but.. It's the only way at the moment, I think.

5
  • The issue is that the URL and the sectionHandle don't necessarily correspond so it's not a reliable way of doing it. Commented Oct 1, 2014 at 12:14
  • 1
    That is true. What about getting the url format through getUrlFormat(), and then get the first segment via the split('/') filter? Commented Oct 1, 2014 at 12:28
  • Yes, that is the approach I have been taking so far. Commented Oct 1, 2014 at 14:02
  • You could also create a separate structure for your index pages, which would allow you to specify an exact url. Commented Oct 1, 2014 at 14:58
  • Or create routes to the index pages. It would be nice to just be able to define an index page within the section configuration though. Something like, "<checkbox> this section has an index page". And "Index page URL format" for each locale. Commented Oct 1, 2014 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.