Can I resolve full URL to some object - entry or category, from inside Twig template?

Mind you, I don't mean searching for something by slug, but by full URL. So, for example, instead of passing 'some-slug' to function, i want to pass 'some-category/some-subcategory/some-slug'.

I need to to this, because i want to create breadcrumbs links based on craft.request.segments object. So, i need to look up for entry object for some-category, then entry for some-category/some-subcategory/ and then for some-category/some-subcategory/some-slug (well, i dont really need last one because its just page im on currently).

  • 1
    In most cases, Craft does this for you, and you'll have access to {{ entry }}, {{ category }}, etc. in your template, already. Where is this URI coming from? Nov 19, 2018 at 21:13
  • I want to create breadcrumb links based on request segments.
    – piotrpog
    Nov 20, 2018 at 19:04

2 Answers 2


According to the EntryQuery documentation, you can search by URI.

The most basic use (searching by the complete URI) would look like this:

{% set entryWithUri = craft.entries({
  uri: craft.app.request.path | literal
}).one() %}

But to progressively dive into the segments (say, to create breadcrumbs like you mentioned), it takes a little more work:

{% set segments = craft.app.request.segments %}

{% for segment in segments %}
  {% set uriPart = segments[0:loop.index] %}

  {% set entryAtPath = craft.entries({
    uri: uriPart | join('/') | literal
  }).one() %}

  {% if entryAtPath %}
    {{ entryAtPath.link }}
  {% endif %}
{% endfor %}

Supposing entryAtPath is empty, you may then need to fall back to a Category query, then a Tag query, etc. Unfortunately, there isn't a great way to fetch across Element types—which would make this a little easier.

  • Great piece of code, thanks! I will be using it in my boilerplate template from now on. I would only change one thing - to decrease number of queries, i would add if not loop.last - last part of breadcrumb is always current category/entry so there is no need for query. One question - what does literal filter do in this situation?
    – piotrpog
    Nov 20, 2018 at 23:48

Usually I have a {{baseUrl}} defined in config or .env available globally; then {{baseUrl}}/{{entry.uri}}

  • I think the question is about how to look up an entry by a full URI, not how to get the complete path/URI of an entry. Nov 19, 2018 at 21:10
  • Yeas, thats what i want to do @AugustMiller
    – piotrpog
    Nov 20, 2018 at 19:07

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.