Hot answers tagged

24 votes
Accepted

Removing whitespace from {{ tag.name }}

There are multiple ways to solve this: Native: Twigs url_encode filter: {{ "apples and carrots"|url_encode }} {# outputs "apples%20and%20carrots" #} Twigs replace filter: {{ "apples and carrots"|...
user avatar
  • 8,316
18 votes

Remove the last comma in an inline list of tags?

There's a couple of special variables available while for looping through an array. In your case I'd make use of loop.last. {{ tag }}{{ not loop.last ? ', ' }} This is the expression using the ...
user avatar
  • 35.8k
15 votes
Accepted

How can I create a visually weighted tag cloud?

Brandon commented on getting tag usage count (and how it can be expensive) in this Google+ post and again in this SE question where I was trying to think through a plugin. So to completely steal from ...
user avatar
  • 3,921
14 votes

How can I create a visually weighted tag cloud?

Marion Newlevant had a clever, efficient idea in this Straight Up Craft Hangout if you don't mind having a slight lag (like a day) for your tag counts to be up to date. That idea: store the counts you ...
user avatar
  • 3,921
12 votes

Removing whitespace from {{ tag.name }}

Craft has a filter for "Kebab Case", which I think is what you need. It strips out punctuation and replaces spaces with dashes. Useful if you need to format a field as a HTML attribute. {{ tag.name|...
user avatar
  • 1,075
10 votes
Accepted

Remove the last comma in an inline list of tags?

An alternative, if you are getting the tags from the Tag Field type, is the Join filter: {% if post.tags | length %} Tags: {{ post.tags | join(', ') }} {% endif %}
user avatar
  • 320
9 votes

Removing whitespace from {{ tag.name }}

I'm not sure if this feature is newer than this question but you can also say {{ tag.slug }} to get apples-and-carrots. All letters become lowercase as well so the result is more "scripting-friendly".
user avatar
7 votes

Order tags by count - e.g. Top 10 Tags Used

This is based on Matt's answer to your previous question. Likewise this should better be used within cache tags! This is untested, but it hopefully works out for you. . Edit: This is still ...
user avatar
  • 35.8k
6 votes
Accepted

How do I display an entry's tag?

To list all tags on a particular entry use: {% for tag in entry.tagField %} {{ tag }} {% endfor %} If you only want the first tag you can just use: {{ entry.tagField.first }}
user avatar
  • 2,985
6 votes
Accepted

Looking to use a tag slug/URI and loop through entries of that tag

Here's a really DRY way to do it. Setup a route, something like "news/tags/(tag)" to load the "news/index" template (the same one used for listing all entries). In the "news/index" template: {# If ...
user avatar
  • 2,985
6 votes

How do I delete a tag?

Currently it’s not possible to delete tags (or otherwise manage them).
user avatar
6 votes
Accepted

Show a list of recently used tags only from the current locale

Brandon at P&T was able to fill in the piece I was missing. He said: To only get tags that were related from a specific locale, you just need to specify that in your relatedTo param: {% set ...
user avatar
6 votes

How do I display tags comma separated with "and" between the last two tags?

Not very elegant, but... :) {% for tag in tags %} {{ tag.type }}{{ loop.index < loop.length-1 ? ', ' : loop.index < loop.length ? ' and' }} {% endfor %}
user avatar
  • 7,238
6 votes
Accepted

How can I group a list of tags into ranges of first letters (A–E, F–J, etc.)?

You can do this with the twigPCRE filter. Once you have your allTags, group them like this: {% set allTagsGrouped = allTags|group('name|first|upper|preg_replace("/[A-M]/", "A-M", 1)|preg_replace("/[N-...
user avatar
6 votes
Accepted

How do I make the "%20" in URL's show as "-"

Edited I just learned this, but as of Craft 2.4, there's a kebab filter which you can use here. This would probably give you the effect you're after, where the string "two words" would become "two-...
user avatar
6 votes

Get the Category name into the URL for an entry

A Categories field can be related to several categories. So you'll either need to limit your Category field to a single Category relationship or you'll need to make some assumption in how you build ...
user avatar
  • 13.2k
6 votes

Deleting unused tags

There currently isn't any Tag management in Craft, but if you think there should be, consider voting for this feature request. In the meantime, there is a 3rd party plugin called Tag Manager that ...
user avatar
  • 66.4k
6 votes
Accepted

How do I add a value to a tag field type using the entries service saveEntry method?

The Tags field classifiedTags needs an array of tag IDs, not slugs/titles. Also, if this is an existing entry and you want to retain tags already added to the field, you'll need to pull the existing ...
user avatar
5 votes
Accepted

How can I best extend TagModel to add a `count` property?

Before worrying about how templating should work, let’s just get one thing out of the way: Computing this data every time it’s requested is horribly inefficient, and could easily take a site down. You’...
user avatar
5 votes

Is it possible to call a variable within a tag pair?

Try this: {% for block in availability[foo] %} The square brackets let you access a variable variable name on an object. As a general rule you never put {{ }} inside of {% %} tags in twig.
user avatar
5 votes
Accepted

How do you rename a tag globally in the Craft Control Panel?

As of Craft 2.0 you can rename tags by double-clicking on them.
user avatar
5 votes

Search for related entries based on tags

This should work: {% set tags = entry.q_tags %} {% set entries = craft.entries.relatedTo(tags).order('score').find() %}
user avatar
  • 6,288
5 votes

Remove the last comma in an inline list of tags?

If you you have a given string and can't influence how it is generated, use the slice filter to crop that last comma: {% set tags = 'Alpirsbacher Klosterbräu, Rothaus Tannenzäpfle, Neumarkter ...
user avatar
  • 35.8k
5 votes
Accepted

How to build a sentence based on tags

I think it'll be easier and more efficient to achieve that if you use the loop functionality instead of querying the actual tags. I think this code does what you want: {% set topics = '' %} {% for ...
user avatar
  • 7,238
5 votes

How can I make an oversized chalkboard organiser?

Definitely use a custom Element Type.
user avatar
  • 13.2k
5 votes

Problem with accents in tags & URL

There is a general config setting for it: 'limitAutoSlugsToAscii' => true, Limits automatically-generated slugs to ASCII characters. When there is an obvious ASCII version of a character (e.g....
user avatar
  • 3,664
5 votes
Accepted

.relatedTo() not returning tags with spaces

In your working example, you’re mapping request.tags to the search param, but in the non-working example, you’re mapping it to the slug param. Those two params are handled in very different ways. ...
user avatar
5 votes
Accepted

Query Entries by Tag

You'd need to get the tag model first. {% set tag = craft.tags.search('My Tag').first() %} {% if tag %} {% set entries = craft.entries.relatedTo(tag) %} {% endif %} Replace search with whatever ...
user avatar
  • 35.8k
5 votes

How do I UNLOCK my account?

If you don’t have access to another admin account, you could unlock the account directly in the database. Change the lock value to 0 for the account in the craft_users table.
user avatar
  • 35.8k
5 votes
Accepted

Confused about tags

You told Craft to look for tags/_tag And then you created templates/tags/_tags.html Note you have no s on the end in the route definition, but you do in the template name.
user avatar

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