6

I have a site which has two blog-like sections, which I'll call Blog and News. Each has a few categories. The categories are different in each section. An article always has (only) 1 category assigned to it. I want the URLs to be like:

/blog/                            # index page
/blog/category-slug               # listing all articles in category
/blog/category-slug/article-slug  # an article
/news/
/news/category-slug
/news/category-slug/article-slug

I'm struggling with how to construct this nicely out of Craft's Fields, Sections, Channels, Entries... I imagine I may need some custom routing, but haven't even got that far. Should I have two separate Channels and two separate Category sets? One Channel with two Entry Types and two Category sets? One Category structure with 'blog' and 'news' at the top level? How do I generate URLs for an Entry that will include /news/category-slug/...?

I'm going round and round in circles. I've tried things from answers here but haven't yet managed to display an entry at any URLs I expect.

(As an added wrinkle, I want the /blog/ and /news/ pages to appear to the user to be pages within a separate Structure... but I was going to worry about that once I've got the basic channel/category/entry stuff working!)

1 Answer 1

12

There's a lot here but I'd start by create 2 different channel sections: Blog and News.

From the front end, that makes it easy to see which entries are associated with what sections and easy to post content into each one. It also makes it easy to filter in your templates what section you're after.

To get your entry URLs setup, it's a little tricker but you can pull it off with some Craft/Twig magic. Setup a new field, make the handle blogCategory, make it a Category relational field, lock it down to 1 category that pulls from your Blog Category Group, and assign it as a field to your Blog section.

enter image description here

When you're creating the channel, set the URL format in your Channel to this:

blog/{blogCategory.first().slug}/{slug}

enter image description here

That little bit of code basically tells Craft to use the category slug of the blogCategorycustom field we just created in part of the url and the slug of the current entry to make the url. Craft treats categories, assets, entries, etc. and other relational fields as an "array" even if you only have one selected so we need to use .first() to use the first one Craft sees.

Now when you go create your blog entry, you're able to set a blog category and Craft will also use it as part of the URL.

BTW: There's no problem routing the Blog/News index pages to a page in a structure but you have to do the heavy lifting that Craft usually automatically routes for you.

In your _layout.html template, you could do something like:

{%if craft.request.firstSegment() is "news" %}
    {#load up news template #}
    {% include '_news/index.html' %}
{%elseif craft.request.firstSegment() is "blog" %}
    {#load up news template #}
    {% include '_blog/index.html' %}
{%endif%}

There's more to it than that, of course but hopefully that might get you started. It takes a little while to get your head around Twig and how powerful it is to use in Craft.

You'll want to also setup URLs for your Categories to have Craft load the appropriate template for each. Understanding the basics of how Craft routes requests is helpful as well.

4
  • 1
    This is a really nice and complete answer and I want to add two suggestions, where you could do things a little differently. Instead of using Twig logic to include the section index templates, I'd rather just place the templates in a directory that matches the URI (→ routing case 6). And you could also make the entries load on /blog/article-slug, by leaving out the categoriesField part when setting up the sections' URLs and handle /blog/category-slug/article-slug like URLs with dynamic routes.
    – carlcs
    Commented May 29, 2015 at 8:07
  • This is so great, thank you both, especially RitterKnight for taking all that time. After a morning of fiddling I've got a solution up and running on my site, with index pages in the Structure. I'm using carlcs's idea of having those as templates in URI-matching directories as it fits better with how I've done other things. Thanks again. Commented May 29, 2015 at 12:23
  • BTW, RitterKnight - small typo: the URL format in the Channel is missing a }. Otherwise, great stuff. Commented May 29, 2015 at 12:24
  • Thanks, typo fixed. Nice comment @carlcs, I always forget about #6! What's great about Craft, there's multiple ways to skin the cat, the real problem sometimes ends up just choosing one! Commented May 29, 2015 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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