0

I'm trying to contruct breadcrumbs for entries under a specific section. This feels like such a "how-hard-could-it-possibly-be" type of question, but I've been at this for a lot more time than I ever expected, so finally asking a question here.
So far, this the relevant parts of what I have:

{% macro breadcrumbs(type, entryId) %} 
    {% if type == 'article' %}
        {% set articleEntry = craft.entries.id(entryId).one() %}
        
        {% set section = craft.app.sections.getSectionByHandle('articles') %}
        {% set sectionUrl = siteUrl ~ section.handle|kebab %}

        {{ dd(sectionUrl, section.name) }}
    {% endif %}
{% endmacro %}

This (so far) simple macro is called from the article template.
The dd here outputs the following:

^ "//localhost:3000/sv/articles"
^ "Articles"

articles should really be artiklar in both the contructed URL as well as the name.

I have looked at this question which is 7 years old, but it only fetches the sections "main" name, handle and uri.

articles is a channel, if that matters.

I realize I'm probably going about this issue in totally the wrong way. Feel free to give me pointers on where I'm going wrong.

Edit: @moritzlost made the issue quite apparent: The page that handles the listing/overview of all articles, isn't actually a part of the articles channel. This is of course obvious. I'll leave the question stand in case others end up in the same brain-warp.

What my code ended up looking like:

{% macro breadcrumbs(type, entryId) %} 
    {% if type == 'article' %}
        {% set articleEntry = craft.entries.id(entryId).one() %}
        
        {% set section = craft.entries().section('articlesListing').one() %}
        {% set sectionUrl = section.getUrl() %}
        {% set sectionName = section.seo.metaGlobalVars.seoTitle %} {# Gets the SEO title instead of the default section name. #}

        {{ dd(sectionUrl, sectionName) }}
    {% endif %}
{% endmacro %}

Which yields this:

^ "//localhost:3000/sv/artiklar"
^ "Artiklar om djurhållning"
1
  • What link(s) are you trying to output? What kind of content (section, entry type) are you serving for that link?
    – MoritzLost
    Commented Feb 16, 2022 at 14:52

1 Answer 1

1

A section's handle is the machine-readable identifier for a section, it's not localizable and has nothing to do with URLs of entries in that section. Instead of accessing the section in your template, you need the entries in that section to get their localized URLs.

Since you'te talking about breadcrumbs, I'll assume you want a list of links like this:

/articles/
/articles/article-about-something/

In your example, the current entry would have the URL /articles/article-about-something/ and you're trying to get the link to articles. Now the solution depends on what section the articles belongs to. Since your articles section is a channel (so no hierarchies), I assume your /articles/ pages is another section, probably a single? In this case, you'll want to use an entry query to get that single entry and access its URL, which will be localized automatically:

{% set articlesListing = craft.entries().section('articlesListing').one() %}
{% if articles_index %}
    {% set articles_url = articlesListing.getUrl() %}
{% endif %}

If the /articles/ page is a static route or just matched based on the template name, there's no built-in way to localize it. You'll have to hard-code your local URLs and make sure your routes/template names match all localized slugs.

1
  • 1
    Right you are. /articles/ is of course not part of the section "Articles" at all, but rather it's a single called "articlesListing". So yea, craft.entries().section('articlesListing').one() does indeed give me the right thing. I will fix my question to reflect this serious oversight. Thanks. Commented Feb 16, 2022 at 15:28

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.