1

I have a section where each item has its own URL e.g: news/{slug}

The Entry Template for this section is: news/news_detail

For each post I would like to have a link to a unique page which is populated with information from the fields from the parent post/group, e.g news.

The slug would look something like: news/{slug}/extra

The Entry Template for the news_detail page has a rich text field: additional

To make this more complicated, the link to this page will be visible only if the "additional" field is populated with data. And finally each "extra" page should have a link back to the originating post.

I had considered using a simple JavaScript toggle, but having urls for the content is more important in this instance, so not an option.

Thank you for any pointers as I am not sure where to start.

2

First you'll want 3 templates — I'll call them 'news/index.html', 'news/_entry.html', and 'news/_extra.html'. (fyi... using an underscore before the template name is good practice as it prevents the template from being loaded directly; which would likely throw an error because no entry would be defined.)

You'll then want to create a custom route to the 'extra' page in settings, using the 'slug' token for one of the segments.

  • If your url looks like this: news/slug/extra (where 'slug' is the slug token)
  • Load this template: news/_extra.html

In the '_entry.html' template, you'll want to check your 'additional' field and create a link to your '/extra' page. Note: 'entry.url' should already include the 'news/slug' portion of the uri — we'll just be adding the '/extra' segment.

{% if entry.additional %}
    <a href="{{ entry.url ~ '/extra' }}">extra page</a>
{% endif %}

In the '_extra.html' template, there should now be a 'slug' variable available to your entry.

{% set newsEntry = craft.entry.section('news').slug(slug) %}
{% set newsIndex = craft.entry.section('pages').slug('news') %}
{{ newsIndex.title }} — {{ newsEntry.title }}

Note: I don't know in what section your 'news' index page is. I just picked something at random — you'll need to change that to match your structure.

| improve this answer | |
  • To clarify your final note, the news index page in this example would belong to news. In relation to the actual template folder structure what I have is as follows: root = news.html then a folder called News with two templates inside called news_entry.html and news_extra.html – So in your example you use news/index.html, when it should be news.html – Did this match your understanding? Thank you for your answer and for pointing me in the right direction on this. – Craftnoob May 1 '15 at 1:55
  • Yes. You can change the templates/paths to anything you want. I just used those as an example. I would still recommend placing an underscore in front of any templates that rely on an entry being defined however (i.e. '_news_entry.html' and '_news_extra.html'). – Douglas McDonald May 1 '15 at 2:50
  • I have renamed the last segment of Craft code as follows: {% set newsEntry = craft.entry.section('news').slug(slug) %} {% set newsIndex = craft.entry.section('news').slug('news') %} {{ newsIndex.title }} — {{ newsEntry.title }} - I am not sure where exactly to put this, and know that it is why {{ entry.newsExtra }} isn't loading in any information on the _extra.html page. The url which the user clicks on to see the extra page reads _extra.html - does this have to be the case, can it be made prettier without underscores while also taking your advice at the template side? – Craftnoob May 1 '15 at 4:08
  • The {{ newIndex.title}} and {{ newsEntry.title}} are just an example to show you that you can access those entries as needed from your '_extras.html' template. Change to whatever you need. As far as the uri is concerned—you shouldn't have '_extra' in the uri, only in the route template definition. The uri should be 'news/{slug}/extra' where slug is the title of your news entry. – Douglas McDonald May 1 '15 at 6:27

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.