Hot answers tagged

46

You can use MySQL's RAND() function inside your order method: {% for entry in craft.entries.section('team').type('mainPeople').order('RAND()') %}


35

This is the correct way. Examples: Singles: {% set entry = craft.entries.section('section_handle').one() %} Channels / Structures: {% set entry = craft.entries.section('section_handle').slug('my_slug').one() %} General: {% set entry = craft.entries.id(5).one() %} See craft.entries for a list of all properties.


35

{% set entries = craft.entries({ section: 'blog', order: 'postDate', limit: 5, id: 'not ' ~ entry.id }) %} or {% set entries = craft.entries({ section: 'blog', order: 'postDate', limit: 5, }).id('not ' ~ entry.id) %}


28

Craft documentation describes how to exclude entries with a given value. In this case, we want a string that looks like: 'and, not id1, not id2, not id3, not id4' So we need a string that is all of the featured article ids separated by , not. First, get them as an array using ids(): {% set featuredArticleIds = craft.entries.section('articles').featured(1)....


27

Twig has loop-variables which can be accessed inside for-loops. loop.first will return true on the first iteration. {% for image in entry.images %} <img src="{{ image.url }}" alt="{{ image.title }}"> {% if loop.first %} Do stuff.. {% endif %} {% endfor %}


26

Twig is great for outputting HTML/XML markup, but it’s not exactly the best option when you just want to output some data as JSON. The Element API plugin aims to make outputting entries (and other element types) as JSON as easy as possible, and it’s completely customizable from its PHP-based API configuration file. To achieve your desired JSON output, you ...


26

I just threw together a add-on that will provide a Twig filter called prune to allow you to more easily do what you're asking here. https://github.com/mattstauffer/craftcms-prune With prune you can now just do the following: {{ craft.entries.section('news').find() | prune(['title', 'body']) | json_encode() | raw }} Just pass in an array of the fields you ...


24

This is a known behavioral issue. PHP considers 0, "0", false, and similar values to be “empty”, and Craft only factors most criteria param values into the query if they are not empty. So setting a Lightswitch field’s criteria param to '0' is the same as not setting it at all. The workaround is to use not 1: {% set results = craft.entries.section('events')....


23

If the “loop” is an ElementCriteriaModel object, then you can grab the first element using .first(): {% set blogPost = craft.entries.section('blog').order('posdDate desc').first() %} -- {% set image = entry.myAssetsField.first() %} -- {% set firstTextBlock = entry.myMatrixField.type('text').first() %} However if it’s just a normal array, then you will need ...


22

Twigs date_modify filter accepts every string, which is supported by phps strtotime(). These strings are called Relative Formats. . Examples: In the original post you used: {% set day = date('m/d/Y') %} {% set nextDay = day|date_modify('+1 day') %} but with Relative Formats you can also do something like this: {% set var = day|date_modify('now') %} {% ...


22

According to the docs, you can pass an array into the "section" parameter: {% set entries = craft.entries.section(['quotes','didYouKnow']) %} There are several ways to get a random entry, but the method you've chosen is a very good one, because it randomizes during the initial SQL call: .order('RAND()') Lastly, you can cut out some of the extra legwork ...


19

The ElementCriteriaModel and the individual element models (like the EntryModel) can be thought of as two different points of time in retrieving data from your database. Essentially, you'll use an ElementCriteriaModel before the database is queried and the element models afterwards. Why do I sometimes have to fetch elements with .first(), .last(), .find()…...


18

You're looking for the dateUpdated property. {{ entry.dateUpdated }}


18

I'd suggest changing the Recipe section to a "Structure" section type. This lets the client order the entries with a drag and drop from the Entries tab where they can see the whole list of entries during the reordering.


17

1. "Expected name or number" error The parser is expecting a method/property after the dot of productSale, but you are passing arguments. Removing the dot to make it valid: {% set products = craft.entries.section('product').productSale(':notempty:') %} . 2. Documentation There is no documentation on this yet, but it is listed in the release notes: ...


17

If you want to order by multiple columns that include custom fields, first make sure you're at least on Craft 2.4.2684, then you'll be able to use this syntax: {% for gallery in craft.entries.section('Gallery').order('postDate desc, available desc').limit(null) %} Also note that that you should be using limit(null) instead of limit(none) if you want to ...


16

When viewing an entry, click the arrow on the save button and you'll see an option to "Save as new entry". This duplicates the entry and you can immediately begin editing the new entry. I'm not sure if this is what you mean by "use one article as template for the others" or not. It seems you can either manually create a new entry and then copy and paste ...


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


14

It’s currently only possible via direct manipulation of the entries in your database. You will first need to visit your sections table to determine the desired section ID, and then the entrytypes table to determine the desired entry type ID. Once you have those, find your entry’s row in the entries table, and change its sectionId and typeId columns ...


14

I just wrote a plugin that deletes all versions (truncates the craft_entryversions table) and then saves a new version for each of your entries, so you can later revert back to it: Delete All Entry Versions


14

You're actually closer than you might think. All you need to do is add your field handles as parameters. I prefer to use the hash style of declaring parameters but either way will work, I just find this easier to read. {% set params = { section : 'events', order : 'eventDate asc', eventDate : '>=' ~ now, eventType : 'green', limit : '10' } %} {...


13

The data in Craft is handled mostly by Models (and arrays of Models). Before pulling actual information, you define your criteria in an ElementCriteriaModel using generic parameters and parameters specific to what you are trying to get (entries, assets, etc.). You then use one of the ElementCriteriaModel's functions to pull the actual data. You will get ...


13

The for tag has an else clause by which you can render a replacement block if no iteration took place because the sequence was empty. <ul> {% for user in users %} <li>{{ user.username|e }}</li> {% else %} <li><em>no user found</em></li> {% endfor %} </ul> reference: http://twig....


13

Read the article about ElementCriteriaModel in the official Craft docs on how to query elements in general and the craft.entries article about the parameter available to modify the object returned. Example code for your events list: {# Get events from `events` section in desired sort order #} {% set allEvents = craft.entries.section('events').order('...


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


13

You should be using setContentFromPost() rather than setContent(), so that field types have a chance to prep the values that you are passing in. See “What is getContent() / the ContentModel, and how do I use it to get and set custom field values?” for more info on how this stuff works.


13

Set the limit to null limit(null).


12

There are several ways you could attack this, but probably the easiest is directly from the SQL side with something like this using a SQL CASE statement: {% set entries = craft.entries.section('news').order('(CASE WHEN field_alphaTitle IS NULL OR field_alphaTitle = "" THEN title ELSE field_AlphaTitle END)').find() %} <ul> {% for entry in entries %...


12

This is not the answer you were looking for, but if you decide to just go with a normal drop-down, then you can paste the following into the dropdown field in the database. This would go in the craft_fields table, yourCountryField row, settings column. After editing the list the way you need it I would suggest removing all line-breaks using grep find/replace ...


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


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