James Smith
  • Member for 7 years, 7 months
  • Last seen this week
How to write a simple navigation?
14 votes

I pulled together this brain-dump of 8 different ways of doing navigation. There's probably more. With any method make sure you keep a close eye on the query count and load speed, as some of these ...

View answer
Events documentation for Craft 3
13 votes

Whilst Doug's answer is useful for knowing the ins and outs of a particular event once you've found it, actually finding which events are available in Craft 3 in the current docs is not straight-...

View answer
How to find next sibling from entry order of parent entries field
Accepted answer
5 votes

nextSiblingOf() and related methods will only work with Structure entries. What you need instead is next() / prev(): https://docs.craftcms.com/api/v3/craft-base-element.html#next (At the time of ...

View answer
Rendering pages from Craft CMS as static HTML
Accepted answer
5 votes

There are a few different solutions for this. Personally my favourite is to switch on static caching in nginx, as it's a relatively quick and easy solution. Andrew has you covered on how to do that ...

View answer
How to check an entry is in a section within a for loop
Accepted answer
5 votes

Check against entry.section.handle: {% for entry in craft.entries.authorId(authorIds).orderBy('dateCreated desc').all() %} {% if entry.section.handle == 'projects' %} ...

View answer
The license located at config/license.key belongs to X
Accepted answer
4 votes

Go into Utilities > Caches > Clear Caches. I can't remember exactly which cache, but try all of them (maybe untick the asset transforms ones so you don't unnecessarily regenerate transformed ...

View answer
Can you prevent upscaling when using Craft's image transforms?
4 votes

As of Craft 3.4 you can set upscaleImages to false in general config: https://craftcms.com/docs/3.x/config/config-settings.html#upscaleimages

View answer
How do I check if a product has stock in any of it's variants?
4 votes

Just in case it's preferred by anyone else, here's a more compact and performant version of pretty much the same technique as Luke's answer: {% set inStock = product.variants|filter(v => v.hasStock(...

View answer
Why is $DEFAULT_SITE_URL appearing in entries URL?
Accepted answer
4 votes

You probably have a misconfiguration in your environment variables - First go to the control panel and check what you have under Settings > Sites > [choose site] > Base URL. If it's set to an ...

View answer
Load More Entries Button
4 votes

Here's a cut-down version of what I'm using on https://cld.agency/showcase It's using jQuery because that's the way I like it: {% set theLayout = craft.app.request.isAjax and not craft.app.request....

View answer
CSS file not being read by other templates
3 votes

Why use includeCssFile at all for your core CSS? Typically you'd use that for injecting template-specific CSS files (e.g. carousel scripts) that you don't want to load on every page. (The advantage is ...

View answer
Is there a "sticky post" function built into Craft?
3 votes

Answer update for Craft 3, and also adding in a secondary orderBy so that remaining entries are still ordered by date: {% set entries = craft.entries.section('news').orderBy('myLightswitch desc, ...

View answer
Loop over query results in a macro
Accepted answer
3 votes

To convert the results of an Element Query into a custom key/value array you can use the index filter to specify what you want to use as the keys. So for example if you want the category slugs to be ...

View answer
Prevent adding users with same email addresses
Accepted answer
3 votes

You're passing false as the second parameter of your saveElement call... that parameter means you're skipping the built-in validation which should catch this error for you. If you get rid of that you ...

View answer
Getting entries excluding the first
Accepted answer
3 votes

"Measure twice, cut once" goes the saying... (Kind of the opposite here, but the same principle!) - Fetch your data once, then cut it up how you like using Twig filters. In your case you can ...

View answer
Dynamic URL generated by a custom field dropdown value
Accepted answer
3 votes

In the section settings, you can specify a dynamic entry URI format. As the docs say, this box is in fact a mini Twig template, and is preloaded with the element being saved (accessible as object) and ...

View answer
Dealing with missing images
3 votes

Craft 3.5 introduced a new config setting - brokenImagePath to deal with exactly this scenario. https://craftcms.com/docs/3.x/config/config-settings.html#brokenimagepath

View answer
Limiting entries object when using as json_encode
3 votes

As per the docs: Element queries are specialized query builders under the hood, so they support most of the same methods provided by craft\db\Query. ...So, in your case you could use select() to ...

View answer
Calling a setter via craft.app in Twig
Accepted answer
3 votes

I'm curious to know your use case for this, but have you tried using Twig's do tag? https://twig.symfony.com/doc/2.x/tags/do.html i.e. {% do craft.app.session.set("mySession", "...

View answer
How to check an elements elementType
3 votes

Having arrived here from a Google search while trying to do this in Twig rather than PHP, I thought it might help someone in future to leave an answer, even though it doesn't address your question ...

View answer
Help understanding hooks
3 votes

First I think it helps to clear up some terminology... I've seen a few devs (myself included sometimes!) using the word "hooks" when they really mean "events". For me at least this ...

View answer
Is this a basic error, or do I misunderstand how Craft queries the database?
Accepted answer
3 votes

You're not misunderstanding anything, and your concerns are perfectly valid. I feel like many developers using Craft do not realise that .one() and .all() are in fact database calls, and pretty ...

View answer
Can you call a controller method from within a plugin/module in Craft 3?
Accepted answer
3 votes

Based on your goal, I don't recommend taking this approach for the technical debt reasons in my comment, however your actual question is still a valid one. You can call any controller's method from a ...

View answer
Offset setting a limit?
3 votes

Joshua M's answer explains why it's happening, but in terms of best practice, you'd be better off avoiding multiple roundtrips to the database by using Twig filters on a single result set. There are ...

View answer
Get field type in templates
Accepted answer
3 votes

There might be other ways, but here's one possibility: {{ craft.app.fields.getFieldByHandle('yourFieldHandle').className() }} This will return a string of craft\fields\PlainText, craft\fields\...

View answer
Output specific entry from entries field based on position
Accepted answer
3 votes

{% set allEntries = entry.entriesSelector.all() %} <ul> <li>First: {{ allEntries[0].title }}</li> <li>Second: {{ allEntries[1].title }}</li> <li>Fourth:...

View answer
Dynamically populating select input fields using jquery $.load and twig template
Accepted answer
3 votes

Your code looks fine and worked for me when testing locally. However it could be failing due to the fact that you're not url-encoding the parameter before you send the request. If the values contain ...

View answer
Exclude most recent entry type from a loop
Accepted answer
3 votes

Best practice is to only query for the data once, then perform any necessary filtering on the result, rather than making multiple expensive calls to craft.entries... So in your case you could take ...

View answer
How to include twig in the editor? e.g. Shortcodes for dynamic content
3 votes

The typical 'Craft' way of doing this would be via a Matrix field instead of a single text block field, so that editors can sandwich the dynamic content (which would be its own block type) in between ...

View answer
Add home page to a structure?
3 votes

From an IA and content-modelling perspective, putting all your site's "pages" in a Structure is fraught with problems, and, in my opinion, does a terrible disservice to the future-friendly approach ...

View answer