Hot answers tagged

16 votes
Accepted

Show related entries without current entry

You're wanting to show a paginated list of related entries from a single entry page based on a tag relationship, correct? This should point you in the right direction: {% set segment = craft.request....
user avatar
  • 1,909
13 votes
Accepted

Is it possible to output the name of a Matrix Block?

block.type is actually an alias for a getType() function, which returns a new MatrixBlockTypeModel object. Those will output their handle by default, which is why it's possible to compare block.type ...
user avatar
12 votes

How to output a field as raw HTML?

The difference between "raw" and "escape" is sometimes confusing... Try this instead: {{ element.blockContent|e }} escape: The escape filter escapes a string for safe insertion into the final ...
user avatar
  • 23.6k
11 votes
Accepted

Display entries if Field Date is past Now

You can combine a for and if statement in twig, so you don't necessarily need to use search. Something like this might get you a bit closer: {% for entry in craft.entries.section('event') if entry....
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
10 votes
Accepted

How to load entries via ajax when a filter is clicked?

There's a ton of ways to pull this off, but in my opinion setting up pages/URLs for each category is probably a good idea, for SEO and sharing reasons. It's not something that requires a lot of work ...
user avatar
10 votes
Accepted

How to order or filter entries by their author's custom field?

This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query. We first perform an inner join on the author's content table using the authorId so that the ...
user avatar
  • 6,341
8 votes
Accepted

How to output a field as raw HTML?

For what it's worth, the problem was due to a mistake on my part: the field in question had been set up as Rich Text, and then switched to Plain Text. I guess the original HTML had been encoded going ...
user avatar
  • 1,215
8 votes
Accepted

Alphabetical index for a list of entries

First, sort all entries by multiple criteria: {# get our entries and order by firstname, lastname #} {% set allEntries = craft.entries.section('entries').orderBy('lastname asc, firstname asc').all() %...
user avatar
  • 589
7 votes
Accepted

Custom Plugin implementation

You've got a little ways to go before that will work as a plugin... I'd highly recommend getting a little more familiar with the basic anatomy of a plugin... Craft Docs - Anatomy of a Plugin ...
user avatar
  • 23.6k
7 votes
Accepted

Element API - 'String' to 'kebab-case'

You can actually use the StringHelper::toKebabCase function for this: $string = 'Hello there'; $result = StringHelper::toKebabCase($string); // => 'hello-there'
user avatar
  • 876
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
Accepted

Why isn't my Twig replace filter working with craft.request.getUrl()?

Try: {{ pages|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }} You were double nesting Twig tags {{ {{ }} }}, which is never necessary. You'll want to use string ...
user avatar
  • 66.5k
6 votes
Accepted

Exclude category from channel entries (with pagination)

The without filter converts your ElementCriteriaModel into an array of entry models (the same as find() does). But that array won't work with paginate, as explained in the docs: This parameter ...
user avatar
  • 35.9k
6 votes
Accepted

Add batch filter as variable

Thought I would add this as an answer instead as it should solve your issue. I think the issue with writing it like batch('{{ nr }}') is that twig sees whatever is in '' as a string, so is seeing {{ ...
user avatar
  • 4,519
6 votes

How to apply multiple Twig filters to text - SOLVED

I believe filters are applied in the order they're written. So in your example you're probably applying the new line breaks with nl2br and then removing them again with hacksaw. Try: <p>{{ ...
user avatar
6 votes

Display entries by category (multiple categories)

You can use .relatedTo(category) {% for category in craft.categories.group('categoryName') %} <h2>{{category.title}}</h2> {% for entry in craft.entries.section('news').relatedTo(...
user avatar
5 votes
Accepted

How to split a string into an array

You would use the split filter. {% set fruits = "Apples; Bananas; Tomatoes" | split('; ') %}
user avatar
5 votes

How to limit number of entries returned from a group filter?

The reason limit() isn't working is that is a method on the ElementCriteriaModel you get from craft.entries. Because you're actually dealing with a standard array when you access catEntries, what ...
user avatar
  • 4,361
5 votes
Accepted

How do you wrap text with HTML within a Plain Text field?

You can use the raw Twig filter. Try this: {{ product.name | replace({'®': '<sup>®</sup>' })|raw }}
user avatar
5 votes
Accepted

Filter entries by entry type in the CP?

What you can do is to create additional entry sources using the modifyEntrySources hook. This is a very simple example, that will add a heading and two sources to the end of the sources list (below ...
user avatar
  • 35.9k
5 votes
Accepted

How to display the title of a category based on its ID?

First you’ll need to grab the category ID from the query string, using craft.request.getQuery(): {% set catId = craft.request.getQuery('catId') %} Then you can fetch the CategoryModel via craft....
user avatar
5 votes
Accepted

How do I use URL parameters to filter a channel by a date field?

You can pull query string variables using the HttpRequestService, like this: {% set month = craft.request.query('month') %} {% set year = craft.request.query('year') %} Then, you'll need to ...
user avatar
5 votes
Accepted

Count all unique values

Something like this should do the trick: {% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country') %} <ul> {% for country, users in usersGroupedByCountry %} ...
user avatar
5 votes
Accepted

How to filter entries based on Tags

You can combine multiple relationship criteria by passing an array to the relatedTo parameter with the keyword 'and' or 'or' as the first array element. You want your entries to be related to any of ...
user avatar
  • 35.9k
4 votes
Accepted

Query for blocks across Block Types using the new field parameter?

Unfortunately this is a limitation of that feature. In this case, both of your Lightswitch fields will be tweaking the DB query with their own WHERE [field_column_name] = 1, without knowledge of each ...
user avatar
4 votes
Accepted

Craft or Twig filter for MD5 or SHA3 hash creation?

sounds like a simple plugin will do the trick for you. You will need to set up your main plugin class and a variable class. Your variable class will look something like this: <?php namespace ...
user avatar
4 votes
Accepted

How do I get entries of a child category, related to a parent category, defined in a URL segment?

I don't think that there's a "relation" between categories and their sub-categories. But that's what you currently have as a parameter on craft.categories. Use the ancestorOf() parameter instead to ...
user avatar
  • 35.9k
4 votes
Accepted

Twig Filter in Plugin/PHP?

In one of your plugin services, you can render a template that handles the markdown filter and return the results as an HTML string. You can then send that HTML to wherever you want to preview. In ...
user avatar
4 votes
Accepted

Filter by multiple categories

Craft's relatedTo takes a model, an ID, or an array of either of these. So if you're passing '171,172,179' It's probably coming through as a string and only reading the first ID. Try making it an ...
user avatar
  • 1,828

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