27 votes
Accepted

How do you list categories in a category group?

You can fetch your categories on the front end using craft.categories, which has a group param that can be used to filter categories by a given group handle. {% set categories = craft.categories....
user avatar
24 votes
Accepted

Get category title by slug

You need to add .first() when setting the category. As far as I can tell, if you don't have .first() only {{ category.slug }} will work. First get the slug: {% set catSlug = craft.request.getSegment(...
user avatar
  • 2,985
20 votes
Accepted

Users relatedTo multiple categories

Try passing your relatedTo parameters as an array: {% set members = craft.users.group('members').relatedTo([ 'and', { targetElement: services }, { targetElement: locations } ]) %} or a little easier ...
user avatar
  • 35.8k
18 votes
Accepted

How do I add a subcategory to the URL slug for a channel entry?

If you go into your Category Group’s settings and set the following settings: Craft 2 Top Level URL Format: {slug} Nested URL Format: {parent.uri}/{slug} Craft 3 Category URI Format: {parent.uri}/{...
user avatar
15 votes
Accepted

Get entries which have a certain category assigned to them

This should do what you are looking for: {% set category = craft.categories.slug('family') %} {% set entries = craft.entries.section('images').relatedTo(category).find() %} {# If there are any ...
user avatar
  • 3,594
13 votes
Accepted

Check to see if category is populated?

There is a great tutorial over at Straight Up Craft for handling this (it is however very similar to what you have - it may speed things up a tad though). First, we want to grab all of the entry ...
user avatar
  • 1,909
12 votes
Accepted

Listing categories with post count in a nav?

Within your nav loop, you would use the method count() with an craft.entries Element Query, to only count the entries that are related to the category you're looping. Pass the category object to the ...
user avatar
  • 35.8k
12 votes
Accepted

Showing categories in Channel Entry URLs

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 ...
user avatar
  • 6,467
11 votes
Accepted

How to find entries with no category

You can do this in a single query using the search parameter: {% set uncategorized = craft.entries.section('news').search('-categories:*') %} More on searching here.
user avatar
  • 6,226
10 votes
Accepted

How to list entries related to a category + related to a section?

When you call this: craft.entries({section: 'games'}).relatedTo(category).relatedTo(craft.entries.section('reviews')) that second relatedTo param is just overriding the first one. Lucky for you, ...
user avatar
10 votes
Accepted

How to filter entries by clicking multiple categories on front end

First, I would use something like training in your query string. It's a little more readable and you aren't searching for the words lunge and dumbbell anywhere in those entries, just entries related ...
user avatar
  • 2,573
9 votes
Accepted

How do I list category name?

An ElementCriteriaModel is "used to fetch elements with a given set of parameters." {{ entry.blogCategory }} hasn't yet fetched your category element. You need: <p class="category">{{ entry....
user avatar
9 votes
Accepted

Display entries that belong to a category

Categories are Craft's implementation of a taxonomy, which itself is the classification of things into groups and sub-groups. In this respect, I suppose it makes sense that if an entry is related to a ...
user avatar
9 votes
Accepted

How to loop through category group and identify those related to entry

Your solution looks like it's almost there, but it would be making a lot of DB queries as Craft will be querying the database for each category. You can give Craft all the help you can with something ...
user avatar
9 votes

Listing categories alphabetically & total post count

The relatedTo param will do the trick: {% set entries = craft.entries.limit(null) %} {% for category in craft.categories.relatedTo(entries).order('title asc') %} {% set entryCount = entries....
user avatar
  • 2,985
9 votes
Accepted

Is it possible to have category listing page of entries if the category is defined within a matrix block inside said entries?

First you need to define the category. If your category group has the “Categories in this group have their own URLs” checkbox checked, and the URL Format is set to category/{slug}, and this is ...
user avatar
9 votes
Accepted

Order entries by category field

Here's a solution using the group filter. {% set entriesGrouped = craft.entries.relatedTo(category).order('title')|group('myNewCategoriesField.first().title') %} {% for group in entriesGrouped %} ...
user avatar
  • 35.8k
9 votes
Accepted

Order by score and another field

I assume you're using .search() – a common mistake is to try to combine orderBy('score') with custom fields, which doesn't work because the score attribute isn't an actual field, but a computed value ...
user avatar
9 votes
Accepted

URI for nested categories in Craft 3

You can actually use a bit of Twig logic to determine your category URI formats. Try this for your Category URI Format: {parent ? parent.uri : 'what-we-do'}/{slug} Which is just a more compact way ...
user avatar
  • 2,573
8 votes
Accepted

How do I output entries by their category?

It might be quicker to turn it around: first get all the entries you need, then group those by their category: {% set entries = craft.entries.section('people').find() %} {% for category, catEntries ...
user avatar
  • 6,288
8 votes
Accepted

Building a simple search by multiple categories

Building the form is pretty straightforward, you get the categories from your "locations" and "types" category groups using craft.categories and loop them to build your select options. I choose to ...
user avatar
  • 35.8k
8 votes
Accepted

Craft entries related on a category page

I should note that the relatedTo method accepts a CategoryModel, not the title of a category. What you ended up doing works, but here's the way to do with the relatedTo method, which is what I'd ...
user avatar
8 votes
Accepted

How to get entries related to current category only (not children)

Since Apples and Bananas are children of Fruit, all entires that are related to Apples or Bananas are also related to Fruit, which is why you are getting the the results you are getting. If you need ...
user avatar
8 votes
Accepted

Craft 3 Multi Category and Field Search

Solved this by merging the search parameters together for both the multiple categories and the number fields if they were set in the url parameters. This solves the loop.last pagination problem as ...
user avatar
8 votes
Accepted

Get related posts excluding current post in Craft 3

To exclude the current post use the current post's id with a "not" clause in the id query param. {% for relatedPost in craft.entries.relatedTo(entry.newsCategory).id('not ' ~ entry.id).limit(5).all() ...
user avatar
  • 2,717
7 votes
Accepted

What is the difference between 'nav' and 'for' for looping?

for is part of the native Twig language. for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in ...
user avatar
  • 23.6k
7 votes
Accepted

Ordering of products?

Product types are really all about data concerns - so if the data you're storing for your canines/equines (hereafter known as dogs and horses to save some typing ;) ) - is the same, then you're right ...
user avatar
7 votes
Accepted

Custom Category order in a Categories Field

No, it’s not possible to change the order of related categories. (Think of the field as a big checkbox list; you can change which checkboxes are checked, but you can’t change their order. The only ...
user avatar
7 votes
Accepted

Filtering multiple categories with URL queries

The issue with your current code is that you're looping over each type/size category, creating a parameter in your relatedTo query for each one. This means that you're currently creating a query that ...
user avatar
6 votes

How can I create a simple loop through entries in a category?

Here's another good example for working with categories that may be useful to you. This will loop through all categories (that have entries) and show their entries: {% set entries = craft.entries....
user avatar
  • 2,985

Only top scored, non community-wiki answers of a minimum length are eligible