Hot answers tagged

16

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.lastSegment %} {% for entry in craft.entries.slug(segment) %} {% set tagId = entry.myTagFieldHandle.first() %} {% set relatedEntries = craft.entries....


13

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 against block type handles. But MatrixBlockTypeModel also has a handle property, so this is totally possible: {{ block.type.handle }}


11

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 output. raw: The raw filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be ...


11

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.postDate < now %} {# output your event here #} {% endfor %} It'll just output what meets the criteria if your if statement. The bonus is it doesn't mess ...


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

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 custom field is available to us. in Twig: {% set elementQuery = craft.entries() .section('sectionHandle') .with(['author']) .innerJoin('{{%content}} ...


7

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 into the database when the field was still Rich Text, and so was already entity-encoded before it even reached the template. When I pasted the HTML in question ...


7

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 Basically, you'll want to take the "task" method that you have and move it to a *Variable.php file. Then you will be able to call that variable via a Twig template... ...


7

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 and would make implementing the AJAX part a breeze. Another win is that you're going to have a solution which will actually work without AJAX or JavaScript, too –...


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


6

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 concatenation instead. Also for dynamic keys in twig you need parentheses.


6

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 needs to be an actual ElementCriteriaModel object; not an array of elements. So don’t call find() on the object. So you need to query for the entries you're after ...


6

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 {{ nr }} as it is. You also don't need to echo a variable as a function parameter i.e batch({{ nr }}), you just need to reference the variable itself. So by just ...


6

You can actually use the StringHelper::toKebabCase function for this: $string = 'Hello there'; $result = StringHelper::toKebabCase($string); // => 'hello-there'


6

You can use .relatedTo(category) {% for category in craft.categories.group('categoryName') %} <h2>{{category.title}}</h2> {% for entry in craft.entries.section('news').relatedTo(category) %} <a href="{{entry.url}}">{{entry.title}}</a><br> {% endfor %} {% endfor %} You can check for length if you don't want to show ...


6

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() %} Then use craft's custom "group" filter (lastname[:1] picks the first letter): {# group them by the first letter of the last name https://docs.craftcms.com/...


5

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 you want instead is Twig's |slice(start, length) filter: {% for entry in catEntries|slice(0, 4) %} // You'll only get four entries shown here {% endfor %}


5

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


5

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.categories and its id param: {% set category = craft.categories.id(catId).first() %} We can’t be sure that the ID was actually valid, so make sure that category ...


5

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>{{ workItem.summary | hacksaw(words='75', append='...') | nl2br }}</p> Alternatively you could try setting hacksaw to allow line break tags. Something like ...


5

Something like this should do the trick: {% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country') %} <ul> {% for country, users in usersGroupedByCountry %} {% if country and users|length %} <li>{{ country }} ({{ users|length }})</li> {% endif %} {% endfor %} </ul> ...


5

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 the tags, so this should work for you: {% set query = craft.request.getQuery() %} {# Make sure selectedTagIds is an array #} {% set selectedTagIds = query.tags ...


4

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 filter your subcategories: {% set mainCategory = craft.categories.group('section').level(1).slug(secondSeg).first() %} {% set secondLevelCategories = craft....


4

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 Craft; class YourPluginNameVariable { public function makeHash($string) { $hashed = md5($string); return $hashed; } } You can call ...


4

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 your controller: public function actionPreviewContentAjax() { $this->requireAjaxRequest(); $content = craft()->request->getPost('...


4

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 array and looping through the values: {% set catPars = craft.request.getParam('cats')|split(',') %} {% set related = ['and'] %} {% for catPar in catPars %} {% ...


4

The probably more elegant way to handle this is to not use the without filter at all and simply list the categories you want to exclude prefixed with not. {% set categories = craft.categories({ group: 'location', slug: [ 'and', 'not berlin', 'not amsterdam', ], limit: null, }) %} {% set entries = craft.entries....


4

I had to get this working just the other day. Here is what I did: {# Get the relevant entries #} {% set categoryQuery = craft.request.getParam('category') %} {% set categories = craft.categories.group('sectionName').slug(categoryQuery) %} {# Set the search parameters #} {% set searchParams = { relatedTo: categories, section: 'sectionName' } %} {# ...


4

In my opinion, the first thing you should do is remove all HTML tags. It's not just paragraph tags which could cause you problems... what about <strong>, <a> or <img> tags which get cut off? For legibility sake, let's also break this down into multiple steps: {% set textBlock = entry.bodyContent.type('text').first() %} {% set plainText = ...


4

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


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