I have a bit of code that checks if a page's title has been manually set in a parent template, and if it has to use that, otherwise to use entry.title. This is what that bit looks like:

{% set activeTitle %}
     {% if pageTitle is defined %}
         {{ pageTitle }}
     {% else %}
         {{ entry.title }}
     {% endif %}
{% endset %}

I however have some pages like so:

How do I get the entry based on the parameters that are defined in the URL? And then use this entry's entry.title to set my page title?

  • What do you mean with "'set' 'entry'" and which URL parameter should be relevant? All? I am confused, sorry. And what does this have to do with "entry title" or the code setting pageTitle? – carlcs Feb 16 '15 at 23:38
  • Hey calcs it actually doesn't have much to do with that. I'm running on fumes here. I have a variable "activeTitle" that gets set based on whether there is a manual title 'pageTitle' defined on the parent template or not. If there is not one, it uses the 'entry.title'. I have special case where a query is output 'such as shown in that link' and i need to define what 'entry' that particular query is calling so that the variable 'activeTitle' can be defined. As of now it's a null variable. I basically need to let the page know, what entry should be being called based on the query string in url. – Christopher Healey Feb 17 '15 at 0:05
  • I've been thinking about it a bit more 'still running on fumes' is there an implied for loop on pages that the system knows there is an entry defined for? If that is the case for pages like my special case where the system does not know automatically what entry it should be showing, do i need to create an additional conditional for loop? – Christopher Healey Feb 17 '15 at 0:07
  • I'm confused, too... are you just wanted to manually grab an entry by its Id? set entry = craft.entries.id(170).first() – Brad Bell Feb 17 '15 at 0:35
  • Brad. Essentially yes, that's a part of it. I moved my code a bit to test what the write code is and wrote this: {% set testvariable = craft.request.getParam('entryId') %} <h1>{{ testvariable }} - {% for entry in craft.entries.id(testvariable).first() %} {{ entry.title }} {% endfor %}</h1> and i get an error Impossible to access an attribute ("title") on a string variable ("170") if i put the 'testvariable' in quotes then it doesn't throw an error but no title is output. If i know how to grab an entry by a parameter in the url query i think i could sort the rest of this out but im stumped now. – Christopher Healey Feb 17 '15 at 0:42

This is a longer version answer (code wise) to the one Douglas already posted. He did a very nice job of explaining how to get a URL parameter and what to pay attention to handling the results.

I will try to show you how to build a query from multiple unknown params, so any of these combinations is possible and potentially return a single (and unique) Entry Model:


This also tries to handle the case, the page already comes with an entry variable pre-populated or there's already a pageTitle set. So you don't do all the requests / logic on all of your pages.

 # Only try parsing the params, if this page doesn't come with an `entry`
 # var pre-populated and we don't already have `pageTitle` set.
{% if not entry and pageTitle is not defined %}

    {# Get URL params #}
    {% set entryId = craft.request.getParam('entryId') %}
    {% set entrySlug = craft.request.getParam('slug') %}
    {% set sectionId = craft.request.getParam('sectionId') %}

    {# Default query #}
    {% set query = craft.entries %}

    {# Add params to the query if they are not empty #}
    {% set query = not entryId ? query : query.id(entryId) %}
    {% set query = not entrySlug ? query : query.slug(entrySlug) %}
    {% set query = not sectionId ? query : query.sectionId(sectionId) %}

    {# Now try to get that entry and set our own `entry` var #}
    {% set entry = query.first() %}

{% endif %}

{# Set the entry title. Also handle the case our query didn't find anything #}
{% set entryTitle = entry ? entry.title : 'Title for a page that doesn’t exist' %}

{# Set the page's title #}
{% set activeTitle = pageTitle is defined ? pageTitle : entryTitle %}

Useful links for this code: craft.request and Ternary operator in Twig.

| improve this answer | |

You can access 'post' or 'get' parameters using craft.request.getParam('param'); and then use them to retrieve the corresponding entry. So, given the URL:


In your template, you would grab the param 'id', and use it to retrieve the entry.

{% set entryId = craft.request.getParam('id') %}
{% set entry = craft.entries.id(entryId).first() %}
{% if entry %}
    {% set pageTitle = entry.title %}
{% endif %}

Notice the use of .first(). Results from craft.entries are returned as an ElementCriteriaModel, which is an 'array-like' object, even if only one 'entry' is defined — .first() returns the first element. To avoid errors if no entries are found matching the criteria, check that 'entry' exists before trying to access its properties.

Likewise, do not try and loop through an entry retrieved using .first(), which will also throw an error.

| 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.