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....
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(...
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 ...
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}/{...
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 ...
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 ...
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 ...
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 ...
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.
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, ...
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 ...
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....
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 ...
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 ...
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....
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 ...
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 %}
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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() ...
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 ...
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 ...
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 ...
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 ...
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....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
categories × 500entries × 91
templating × 64
relations × 57
craft3 × 45
category × 39
relatedto × 38
filter × 26
search × 21
structure × 19
matrix × 18
plugin-development × 17
field × 14
tags × 14
url × 13
plugin-craftcommerce × 12
navigation × 11
entry × 10
query × 10
ordering × 10
assets × 9
control-panel × 9
sections × 9
fieldtypes × 9
group × 9