1

I have a site governed by a structure, including the homepage. I have several "parental" landing pages with research areas (Atlantic, Pacific, Midwest etc ) intended to be the gateway to the children entries in that area. (Boston, New York, DC etc).

I have categories (without urls) corresponding to the landing pages (Atlantic, Pacific, Midwest etc ) and would like to pull teasers from each child entry onto the landing page, so that the Atlantic landing page has teasers from the Atlantic children's entries pulled in via the category field, the Pacific landing page has teasers from the Pacific children's entries via category etc...)

But I would like to do this in one template, rather than using multiple entry types.

I know I can get entries by an array of sections

{% for entry in craft.entries.section([atlantic, pacific, midwest]) %}

My mind cannot bend around how I could start at

{% for entry in craft.entries.section('pages') %}

and trek on from there to grab both the appropriate landing page and the categories that go with it dynamically. Can I keep using arrays in both the page id's and the categories? How would can I make a template know to grab the Atlantic and fill it with teasers from the Atlantic category? Is it even possible?

  • How did you set up your landing pages? Singles? And you've set this up to load the same template, right? – carlcs May 7 '15 at 9:39
  • no I have landing pages as part of the structure, too, with their own entry type. And yes, same template . Thanks for the edit, BTW! – Jam May 7 '15 at 23:28
1

You need to find a way to get the right category model to be able to receive all entries related to it. As you didn't set up our categories to have their own URL, you could find it using the relevant slug in your URL. Assuming that maches your categories slugs:

{% set categorySlug = craft.request.getLastSegment() %}
{% set category = craft.categories.slug(categorySlug).first() %}

{% if category %}

    {% set entries = craft.entries.relatedTo(category).limit(12) %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

{% endif %}

Another, probably more elegant way would be to access a token set via a dynamic route:

'regions/[SLUG]' => 'regions/_index'

You would now be able to access that token from a variable "slug":

{% set category = craft.categories.slug(slug).first() %}
  • I have to sit with this for a while and try and wrap my mind around it. New concepts to dig into here! – Jam May 7 '15 at 23:21
  • ok so the top solution worked (lovely!) but I want to make sure I understood how or why. So you said assuming the slug ( in my url ) matches the category slugs, this is relevant to l .getlastSegment( ),? By having matched categories/ landing page slugs, craft grabs the slug from the landing page and sets the category to match the same named slug? Then it gets related entries, loops, outputs and that happy feeling came across me? Do I understand? As more elegant solution, truth be told I don't know what a token set is. Read about routes and ran away waving my hands. TY for solution. – Jam May 8 '15 at 2:13

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.