1

Is it possible to get an entry title and URI from a segment? e.g. If segment 1 has a value of 'about', I'd want to output an entry title of 'About Us', and a URI of 'mysite.com/about', and so on for other segments.

I'm trying to code up a breadcrumb menu, and my site has a mixture of Singles, Structures and Channels.

I have the following _includes/nav_breadcrumb.html template, but I'm hard-coding everything at the moment (and only have it one level deep) so it's not going to be sustainable to keep this up-to-date on anything but the smallest of static sites, hence why I'm wondering about being able to pull in entry details via the segments. Thanks for any help with this.

<a href="{{ siteUrl }}">Home</a> &raquo;

{% if craft.request.getSegment(1) == 'about' %}
  <a href="{{ siteUrl }}about">About</a>

{% elseif craft.request.getSegment(1) == 'news' %}
  <a href="{{ siteUrl }}news">News</a>

{% elseif craft.request.getSegment(1) == 'contact' %}
  <a href="{{ siteUrl }}contact">Contact</a>

{% endif %}
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 up titles and uris based on section/handle, and looping through ancestors within a structure.

You could potentially use the segments themselves, if you where confident that the slugs always matched the titles (or were close enough), and that the uri's where all valid.

{% set segmentUri = "/" %}
{% for segment in craft.request.segments %}
    {% set segmentUri = segmentUri ~ "/" ~ segment %}
    <a href="segmentUri">{{ segment|replace('-',' ')|ucwords }}
{% endfor %}

There is also another technique that involves defining the breadcrumb using an entries field on the entry itself that you might consider — which is described in the documentation on How to create Breadcrumbs.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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