27

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.group('myGroupHandle') %} If the category group is limited to a single level, you can loop through those using a basic for-loop: <ul> {% for category in ...


24

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(3) %} This will give you the CategoryModel object for catSlug: {% set category = craft.categories.slug(catSlug).first() %} You can now access whatever you ...


20

Try passing your relatedTo parameters as an array: {% set members = craft.users.group('members').relatedTo([ 'and', { targetElement: services }, { targetElement: locations } ]) %} or a little easier to read: {% set entries = craft.users({ group: 'members', relatedTo: [ 'and', { targetElement: services }, { targetElement: ...


18

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}/{slug} then you would be able to set your section’s Entry URL Format to this: Craft 2 {myCategoryField.last.uri}/{slug} Craft 3 {myCategoryField.inReverse()...


15

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 entries, loop through them #} {% if entries | length %} {# For each entry we find, do this #} {% for entry in entries %} <p>{{ entry.title }}&...


13

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 ids from the Section of content which we want to display categories for. {% set entryIds = craft.entries.section('blog').ids() %} Next, we will use ...


12

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 relatedTo parameter. {% nav category in craft.categories.group('articleCategories') %} {# Get the count of entries related to `category` #} {% set ...


11

You can do this in a single query using the search parameter: {% set uncategorized = craft.entries.section('news').search('-categories:*') %} More on searching here.


11

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 ...


10

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, that parameter does accept multiple criteria (see the “Passing Multiple Relation Criteria” section on http://buildwithcraft.com/docs/relations#the-relatedTo-param)....


10

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 to those category slugs. {# First you would find any training categories in the URL query string. #} {% set trainingQuery = craft.request.getParam('training') %}...


9

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.blogCategory.first }}</p> if you are positive there is exactly one of them, or more generally: {% for c in entry.blogCategory %} <p class="category"&...


9

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 sub-category, it inherits (it's also related to) its parent. That would explain why you're getting all entries in this way. At the moment, the only way I can ...


9

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 like this. {# First, retrieve all of your events categories #} {% set cats = craft.categories.group('events').find() %} {# Get the IDs of all categories ...


9

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.relatedTo(category).total() %} {{ category }} ({{ entryCount }}) {% endfor %}


9

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 happening on the template you’ve specified in the Category Template setting, then all the stars have aligned and this step is taken care of for you automatically. The ...


9

Here's a solution using the group filter. {% set entriesGrouped = craft.entries.relatedTo(category).order('title')|group('myNewCategoriesField.first().title') %} {% for group in entriesGrouped %} {% for entry in group %} {# ... #} {% endfor %} {% endfor %}


8

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 in entries | group('categoryFieldHandle.first().title') %} <h1>{{ category }}</h1> {% for entry in catEntries %} <a href="{{ ...


8

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 pass the category's slug as the value to submit and /results as the URL to submit to. <form action="{{ url('results') }}" method="get"> <label>...


8

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 suggest for anyone finding this in the future: {% for entry in craft.entries.relatedTo(craft.categories.slug('your-cat-slug')) %} You now have all entries that are ...


8

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 that Craft "injects" into the element model. If I understand you correctly, you want to render any "featured" entries at the top of the search results. You ...


8

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 of doing this: {% if parent %}{{ parent.uri }}{% else %}what-we-do{% endif %}/{slug} That will only add 'what-we-do' to the URI if the current category doesn'...


7

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 to exclude entries related to the subcategories, you can do something like this: {% set dontInclude = [] %} {% for child in craft.categories.descendantOf(...


7

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 other programming languages. nav is defined by Craft, and doesn't exist natively in Twig. nav is special... You'll only need it to loop through entries in a ...


7

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 reason it’s not a simple checkbox list is to save visual space.) If you really want to be able to set the order, you can put the Categories field inside a Matrix ...


7

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 well. Here is the code I used: {# set base params #} {% set params = { section: 'vehicles', limit: null } %} {% set relatedParams = ['and'] %} {# add ...


7

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 looks like this: relatedTo([ 'and', { targetElement: 1 }, { targetElement: 2 }, { targetElement: 3 }, { targetElement: 4 } ]) ...which, ...


7

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() %}


6

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.section('someSection').limit(null) %} {% for category in craft.categories.group('someCategoryGroup').relatedTo(entries) %} <h2>{{ category }}</h2> ...


6

This example assumes you have your Category Group setup to use custom URLs. In the template used to display your categories, the category variable will be available and represent your CategoryModel for that page. The key is to pass the CategoryModel (in our example below category) to the relatedTo parameter of the Entries tag. {{ category.title }} {% ...


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