We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

Category Vs. Section In Craft CMS: Understanding The Difference

10 min read
Category Vs Section In Craft CMS

Navigating Craft CMS's modular content architecture can be confusing for new users. This article will clarify the core differences between categories and sections - two key concepts for structuring your content. Learn how to leverage them appropriately to build a flexible, scalable content model. Gain actionable tips for troubleshooting issues, powering custom displays, and streamlining your authoring workflow.

Categories and sections are key building blocks in Craft CMS for organizing content. Sections define content types and structure while categories offer an additional layer of organization within sections to group related entries. Sections are independent and customizable whereas categories are nested, reliant elements that derive context from their parent section.

Understanding Categories in Craft CMS

What are Categories?

Categories in Craft CMS serve the vital purpose of organising and structuring your content. They allow you to group related entries together under a common category, making it easier for both the site admin and users to navigate the content architecture.

For example, you may have a blog with categories like 'News', 'Reviews', 'How-To Guides' etc. When creating a blog post, you can assign it to one of these categories, which automatically files it under that grouping. From the admin side, you can view, edit and manage all entries belonging to a particular category.

Categories essentially create buckets of content that share some relation or commonality. Readers can click on a category to see all entries filed under it. This offers a clearer content navigation structure compared to everything being jumbled up on the same page.

Other uses of categories include building site navigation menus, filtering search results, segmenting products in an e-commerce store, and creating category-specific RSS feeds among others. The possibilities are endless.

Creating Category Groups

To better organise a large number of categories, Craft CMS allows you to create parent 'Category Groups' under which related sub-categories can reside.

For example, you may group categories like 'Match Reports', 'Player Profiles', 'Club News' etc under a parent 'Football' category group. Or categories like 'Lipsticks', 'Mascara', 'Blushers' under a 'Makeup' parent group.

This allows for a multi-tiered categorisation structure without things getting too cluttered or confusing. To create a new category group, go to Settings > Categories, click on the 'New category group' button and give it an appropriate name and handle.

You can then create individual categories under each group by clicking 'New category' and selecting the parent group it should belong to. The categories will adopt the structure and nest under the parent group, creating a logical taxonomy.

Setting Category Field Options

When creating a category field in Craft CMS, you have several configuration options to customise its settings and behaviour:

Sources - Allows selecting one or multiple category groups that will be available as options for that field. This lets you reuse category groups across different sections or content types.

Limit - Limits how many categories can be selected for that field. Useful for restricting selections.

Allow Limit Overrides - Allows overriding the limit on a per-entry basis. Handy for special cases that need more categories.

Allow Multiple Sources - Allows combining categories from multiple sources in the same field. Adds more flexibility.

Allow Nested Selections - Allows selecting child categories along with their parent groups in one go. Saves time.

Restrict Hierarchy Selections - Limits selection strictly to categories rather than groups for cleaner data.

Show Full Category Path - Displays the full parent-child category path for better orientation.

Show Only Leaves - Only displays categories that don't have any children. Keeps the field uncluttered.

Show Required Category Indicator - Marks categories that have required sub-categories for clarity.

Show Category Descriptions - Shows category descriptions for additional context in the field

Properly configuring these options as per your site's taxonomy structure and content modelling needs goes a long way in enhancing the authoring experience as well as front-end presentation and business logic.

Understanding Sections in Craft CMS

What are Sections?

Sections in Craft CMS essentially define the structure and layout for your content. They allow you to create different content types and modelling schemas through modular building blocks.

For example, you can have separate website sections for blog posts, news articles, products, job listings, etc. Each section will have its own field layout, entry types and content structure.

This provides flexibility to tailor the authoring experience and content presentation for different types of content across your site. Sections let you go beyond the 'one-size-fits-all' approach and enable the creating of customised experiences.

Some common sections include:

  • Blog - For blog posts with fields like post title, body, author, tags, categories etc.

  • News - For news articles with fields like headline, subtitle, article body, author, post date etc.

  • Products - For product entries with fields like title, description, price, reviews etc.

  • Jobs - For job listings with fields like title, location, salary, responsibilities etc.

Sections allow limitless possibilities for modelling your content infrastructure in Craft CMS.

Creating New Sections

Adding a new section in Craft is very straightforward from the control panel. Just go to Settings > Sections and click the "New section" button.

You can then set a name and handle for your section. The name is for internal reference while the handle creates the URL slug (/blog, /jobs etc).

Next, choose the section type - Channel for typical content, Structure for nested or related content, and Single for one-off landing pages.

Finally, customise the section's fields by selecting a pre-made field layout, creating your own custom set of fields, or using an existing one. Fields can be added, removed, reordered as needed.

The newly created section will now be available for creating new entries under its dedicated entry tab. You can create unlimited sections to accommodate all your site's different content types.

Section Types and Field Layouts

Craft offers a few section types to suit different content models:

Channel - The standard section type for regular content like blog posts, news, products etc. Entries have a simple list-based structure.

Structure - For nested, hierarchical or relational content like categories, brand info, docs etc. Entries can have complex structures.

Single - For standalone, one-off pages like contact, about us, homepages etc. Only one Single section entry can exist.

The field layout defines what fields will be available when creating new entries in that section. Field layouts can be customised with different field types, instructions, nesting, grouping etc.

For example, a blog section can have fields like Title, Slug, Author, Featured Image, Post Body, Categories, Tags, Post Date etc.

A jobs section may have fields like Position, Location, Description, Responsibilities, Qualifications, Salary, Company Info etc.

Field layouts can be reused across multiple sections as needed. Craft offers a ton of flexibility for modelling sections and customising their field layouts to match your content types.

Comparing Categories and Sections

Hierarchical Differences

One key difference between categories and sections lies in their hierarchy and structure within Craft CMS. Sections are standalone, top-level entities that exist independently. Categories, on the other hand, are nested elements that reside within the architecture of sections, entries or other content items.

Sections form the foundational building blocks that define and contain content. They are self-sufficient silos that encompass their own content types, fields, and authoring schemas. Categories provide an additional layer of organisation within sections by grouping related content items together.

For example, a 'Blog' section contains individual blog posts as entries. You can then have categories like 'News', 'How-To', 'Reviews' etc within this section to categorise the blog posts. So sections are hierarchical parents, while categories are nested children.

Sections can exist by themselves, but categories are reliant on other elements like sections, channels or entries to reside under. The categories inherit their context and attributes from the parent entity. This is a key structural difference.

Fields and Content Modelling

Another major difference lies in how sections and categories relate to content fields and modelling. Sections define their own bespoke field layouts that accommodate the exact content structure needed for entries within that section.

For example, a 'Jobs' section can have fields tailored to job listings like 'Title', 'Description', 'Location', 'Salary', 'Responsibilities', 'Qualifications' etc. The field layout is customised specifically for jobs.

Categories, on the other hand, do not have their own unique fields. The categories within a section derive their available fields from the parent section layout. For instance, categories within a 'Blog' section cannot define their own custom fields - they adopt the fields defined at the blog section level.

So sections have autonomy in content modelling while categories depend on the parent for field layouts and content structure. Sections allow custom, context-specific fields while categories work with inherited schemas.

Dependencies and Relations

Following on from the earlier points, sections are self-sufficient and standalone while categories are dependent, nested elements. Sections can fully exist in isolation, but categories must be attached to other content structures like sections, channels or entries.

Sections only rely on themselves - they don't need additional elements to maintain their integrity or functioning. Categories don't have this autonomy - they must be associated with a content source to have any meaning or utility within Craft CMS.

For example, a 'Products' section functions perfectly fine alone, but a 'Red' category has no purpose until it is tied to actual products. The categories rely on entities like sections and entries to provide context and offer value.

In summary, sections are the independent building blocks while categories provide an interconnected layer of organisation. Sections form the core structures and categories enhance and extend them through nested relationships.

Using Categories and Sections

Organizing Content

When organizing content in Craft CMS, it's important to leverage both categories and sections effectively to create a robust and flexible structure.

For broader, high-level content types like blog posts, news articles, products etc. sections are ideal for segmentation. Create separate, dedicated sections to accommodate each distinct content format.

Then within sections, use categories to sub-divide and group related entries together for better discoverability and navigation. For example, categorise blog posts under 'News', 'Reviews', 'Guides' etc.

Keep the sections and categories relatively flat for better content orientation. Avoid going over 2-3 levels deep as it can get confusing. Try to maintain under 10 categories per section for optimal usability.

When naming categories and sections, use descriptive, specific names like 'Industry News' or 'Product Updates' rather than generic terms like 'Blog' or 'Articles'. Organise them alphabetically or by popularity/frequency of use.

Regularly review and prune unnecessary/redundant categories and sections to prevent content sprawl. Merge any overlaps between categories or sections to maintain clear segmentation.

Displaying on the Front-end

To display categories and sections on the front-end in Craft, use a combination of tags, variables and category fields in your templates:

  • {{ entry.sectionHandle }} - Displays the section a post belongs to

  • {{ entry.category }} - Displays the categories for that post

  • {% set categories = craft.categories() %} - Fetch all categories

  • {% for category in categories %} - Loop through categories

  • {{ category.title }} - Output category title

You can output categories as text links for navigation. Or use them to filter/group related posts. Sections can appear in page titles, meta info or as parent containers.

For advanced category/section listings, use Craft's Element API to fetch, sort, filter them. Or leverage plugins like Category Map, Scribbler, or Neo for added functionality.

Setting Up URLs

To configure Craft section and category URLs:

  1. Go to Settings -> Sections and click on a section.

  2. Under "Entries in this section have URLs like: " set the URL format.

For example:

  • news/{slug}

  • blog/{postDate.year}/{slug}

  1. For categories, go to Settings -> Categories. Click on a group.

  2. Set the URI Format for that group, like:

  • news/categories/{slug}

  • blog/category/{slug}

This will enable section and category-specific URL structures for easier discovery, sharing, and SEO.

Some tips:

  • Keep URLs clean, short and consistent

  • Use hyphens-over-underscores for readability

  • Include keywords where it makes logical sense

  • Avoid excessive nesting or repetition

Properly configuring URLs for categories and sections in Craft CMS helps build an intuitive, user/SEO-friendly content architecture.

Tips for Developers

Querying Categories and Sections

For developers working in Craft CMS, it's important to understand how to fetch and display categories and sections in Twig templates using the Craft API. The Categories and Sections elements provide helpful methods for querying and surfacing related content.

For example, you can fetch all the categories available in a Craft project using the craft.categories() call. This will return an Element Query that can be looped through to output category details like title, slug, etc. Similarly, individual categories can be queried by their ID or slug using .id() or .slug() methods on the craft.categories object.

Sections can be queried in a similar fashion using craft.sections. The current request section is also available via craft.app.request.getSegment(1) which returns the section handle. This allows conditional logic based on the current section being viewed.

Once you have a category or section element, you can further dig into related entries using chained queries. For instance, looping through a category's entries to output titles, or fetching entries related to a particular section. Element relations open up many possibilities.

The key is to conceptualize categories and sections as structured data points that enable advanced querying capabilities to build customized front-end experiences. The APIs provide powerful content modelling tools for developers.

Plugin Development

When building plugins for Craft CMS, categories and sections provide ample opportunities for customization and extensibility depending on the use case.

For example, a custom section or category element type allows the creation of tailored models with unique attributes and behaviours. A settings model can manage permissions and configuration options for advanced administrative control.

Specialized field types related to categories and sections enable capturing and storing custom data. Utility plugins can offer shortcuts for bulk category or section management. Front-end widgets and display modules can output sections or categories using customized templates and logic.

In essence, by deeply integrating into the categories and sections architecture, developers can provide virtually endless enhancements via custom plugins. The presentation layer can be completely customized using the underlying CMS data structures.

Sections and categories also play an important role in starter themes and site templates by establishing content modelling foundations. Sample sites that incorporate smart categories and section structures provide a great starting point for accelerating development. Overall, the two concepts form the core building blocks for Craft CMS plugins and solutions.

Headless and Decoupled

For headless or decoupled systems using Craft as a backend, categories and sections enable flexible content delivery for frontend consumption.

Since headless CMS separates the backend templates and output from the front-end, it relies on APIs and structured content for delivery. This makes properly designed sections with strategic content modelling extremely important.

For example, an ecommerce headless setup requires a well-planned “Products” section with fields for images, pricing, variants etc to deliver a consumable API output. Similar section planning is critical across all content types.

Categories still provide organizational ability in a headless scenario by segmenting entries and providing hierarchical metadata. For instance, product categories can power faceted search and filtering in headless storefronts.

So while sections and categories don’t directly affect presentation in headless, they allow crafting consumable content APIs that can be flexibly utilized across channels. They add structure and meaning to content graphs.

Troubleshooting Issues

Fixing Broken Links

Troubleshooting and resolving broken category or section links on the front-end requires methodical debugging. Start by inspecting the actual link structure and URLs used compared to the defined category/section URI formats within the CMS settings. Any mismatches in handles, spelling or URL patterns need to be reconciled. If linking to dedicated category/section pages, verify the underlying templates are properly configured and set to the correct entry types.

Sometimes changing category/section handles can cause legacy links to break, so updating any outdated handles is advised. For category links, a good test is assigning entries directly without categories to isolate the problem. Don't forget to submit reindexes, clear caches and restart services after link troubleshooting to refresh all routing.

Browser developer tools are also useful for inspecting HTTP status codes and header responses for any 404s related to invalid links or bad requests. If all else fails, temporarily disable plugins and modules one-by-one until any conflicts are uncovered. With diligent debugging, most category and section link issues can be pinpointed and fixed.

Duplicate or Missing Categories/Sections

Several common culprits can cause duplicate or missing categories and sections in the Craft control panel. Accidentally creating categories with identical names and handles is a prime suspect for duplicates.

Migrating or restoring existing categories without renaming can also produce clashes. Sometimes category synonyms and relations get merged in unexpected ways, resulting in duplicates that need untangling. There are also edge cases of core or plugin bugs accidentally duplicating records.

For missing sections, the usual suspects are accidental deletes or disables, template configuration issues hiding entries, faulty plugin uninstalls removing sections unintentionally, or botched database restores omitting sections. User group permissions can also sometimes mask sections from certain admins.

Fixing these problems involves deleting true duplicate categories, renaming/handling categories intelligently during migrations, debugging category merging behavior, isolating plugin conflicts, double checking section status/visibility settings, verifying user permissions, inspecting template logic, and restoring from a clean database backup.

Entry Display Problems

When Craft entries are not displaying correctly due to the underlying category or section issues, structured troubleshooting helps uncover potential causes. Inspecting the section entry templates and verifying the category/section filtering logic is a good starting point - improper criteria or relations can hide expected entries. Checking that categories are properly assigned to entries is important to avoid missing mappings. Sometimes entries are present but categories or sections are inadvertently disabled or restricted from displaying for certain user groups based on permissions. Testing with different category/section configurations helps isolate any problems.

Switching templates and output methods for category/section pages may reveal specific template conflicts as well. Checking Craft logs for category/section/entries errors provides clues. If direct category/section queries fail, trying alternate parameters helps rule out any quirks. Front-end caching conflicts can also cause seemingly random entry display failures unrelated to underlying CMS categories and sections. With methodical debugging, most entry display issues can be traced back to category/section configurations, relations or templates.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat