Hot answers tagged

18

I would create a Single with the URI set to "404", and also set its Template to "404". That entry will automatically get loaded if you go to http://example.com/404, and load your 404.html template, so at the top of the template you should add this to it: {# # Fetch the 404 Single entry, if we don’t already have it. # (requests to /404 will already have ...


17

My solution is to setup a dedicated "Menu" section and use an entries field type for the links. This allows you to pull together entries from multiple sections into a single menu. My normal setup is to have to custom fields relatedEntry - Entries field allowing you to select any other entry customURL - text field for custom url input The idea being that ...


14

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 methods are very resource-intensive. You can mitigate with the cache tag, but ideally should strive for simplicity where possible... nav approaches Completely ...


14

Yes. Use the RequireLogin tag at the top of a page: {% requireLogin %} This tag will ensure that the user is logged in. If they aren’t, they will be redirected to the Login page, and returned to the original page after successfully logging in. (make sure you are logged out before you test this!)


11

There's no such method available for craft.entries, at least it's not documented. But you can use craft.sections to get an array of section models and filter them with a conditional within a loop: {# Get all Section #} {% set sections = craft.sections.getAllSections() %} {# Loop through all single sections #} {% for singleSection in sections if ...


8

Craft automatically includes the entry variable if the slug matches the url: If the URI matches an entry’s or category’s URI, the section’s/category group’s template will get loaded, and the matched element will be made available to the template via a pre-populated entry or category variable. In your case contact-us gets matched automatically, but ...


7

If you have more complex menus in the future, you should take a look at the Nav-Tag. . Getting singles: There isn't an easy way to get singles (yet). But you can use the same method which is used in crafts backend: {% set allSections = craft.sections.getAllSections() %} {% set singleSections = [] %} {% for section in allSections %} {% if section....


7

In some cases I use a pre-defined (very simple) nav that I don't need to be driven from the CMS yet I want to share it between the header / footer. In these cases I'll set an array in my base layout template so I can use it throughout. In "_partials/layout.twig" template: {% set nav = [ {'title':'Home', 'link':siteUrl, 'class':'homepage'}, {'title'...


7

The performance aspect is difficult to answer. On every pageload, Craft pulls all your global sets, which can be a performance hit, especially if you have many huge, complex global sets with a lot of fields. Any Single used in place of a global could be pulled on a need-to-use basis, i.e. not on every page, although the extra call to the EntriesService would ...


6

You can place your homepage entry in a Structure by setting its slug to __home__. Doing so causes the entry to answer for the / (empty) URI, and it even places the little "home" icon in place of the Entry's slug in the CP. However, if you do this, its best not to nest pages under the homepage, because then their URIs would be of the form: __home__/...


6

Craft creates an EntryModel for each Single Section, that allows you to access the URL property you are after. You can get the (one) EntryModel using the Section's ID and the first() method: {% set singleSectionEntry = craft.entries.sectionId(singleSection.id).first() %} <a href="{{ singleSectionEntry.url }}"> <div class="link">{{ ...


5

If I've deciphered what you're asking correctly, I think the solution you need is to set the single to use your about/index template, not the about/_layout-about template.


4

Another easy way to handle a dynamic menu in Craft is to add a Matrix field to a Global. For example, I have a Global called "Main Menu". That Global has a Matrix field called "Menu Item". I then have a block called "Item" that has two plain text fields. One field is for the "label" and one field is for the "URI" that I want users to be directed to. Then ...


4

1) This is not so simple of an answer. Craft itself uses JS to handle the content of indexes, so I'm not sure how they're doing this. Using what's available you would need to build a list of Single Sections, then pull entries based on that list. {% set sections = [] %} {% for section in craft.sections.getAllSections() %} {% if section.type == 'single' %}...


4

Structures simply add an extra bit of sorting and querying. You can look for entries in a structure the same way you look for entries in a channel. The difference is that you can also have parents and children in a structure with a manually set order. If you look at the section page for a structure and hover over an entry, you will see a move handle. You ...


4

If you just want to set the active class on static nav then you can just create a conditional using any defining attribute (i.e. title, slug, id, uri-segment, etc). Here is an example using the uri-segment: {% set uriSegment = craft.request.segment(1) %} <ul class="primary-nav"> <li><a {% if uriSegment == "about" %}class="active"{% endif %...


4

Updated answer to reflect your added uri requirements. Given your desired uri patterns, I would recommend moving 'subpage-1', 'subpage-2', and 'subpage-3' to the root of your structure which will make all the uri's the way you want without needing to define any custom routes. For auto-generating the navigation, you can then simply add some logic to either ...


4

Take a look into Entry Types, I'm pretty sure that's a much better way to handle what you're trying to achieve: https://craftcms.com/features/entry-types So you'd have a Structure section (let's call it "Pages"), and within that Structure you have multiple Entry Types. You could have a distinct Entry Type for each page type ("About", "Contact", etc). ...


3

I fixed it... I hadn't assigned a field to the section.


3

You don't have to use an extra single section for the homepage, see here: "Add home page to a structure?" Edit: As this question is slightly different to the one I linked to, I will add some more detail on how you can keep your "pages" structure as is. To get the "homepage" entry to load for visits to myexample.com, add a template file with the following ...


3

To get an URL to a specific CraftCMS single entry. Look up for the entry unique id in admin panel and then apply in Twig template: Example below: {% set page_cookies_policy = craft.entries.id(10).first() %} {% set page_terms = craft.entries.id(11).first() %} <ul class="footer-sub-nav"> <li>{{ page_cookies_policy.getLink() }}<...


3

I think there's a few ways you can do this with twig. You should be able to check whether the category slug exists as an attribute on the entry object: {% for category in craft.categories.group('foodMenuCategories') %} {% if category.slug in entry.getAttributes|keys %} ... Or by using the category slug in an array accessor: {% for category ...


3

Ignoring the obvious "use a Structure" answer, I suppose you could fake a "hierarchy" by placing a child and parent entry field on each of your Single sections and manually assigning entries as relationships. Then your Twig template could would just be checking those fields and building the family tree based on the relationships.


3

Most likely you're bumping into php.ini limitations. I'd bump the values of memory_limit, post_max_size and max_input_vars and see if that helps. They are common culprits when POSTing large amounts of data (which is what 450 Matrix Blocks would be considered).


3

Add a /translations/ folder to your project root and within that folder a /fill-in-locale-here/ folder. Add a file "app.php" to that /translations/fill-in-locale-here/ folder which returns an array of those translations. Here's an example => view screenshot


2

404 redirect: You could set up your single to use 404-not-found (example) as a uri, and add a redirect: {% redirect '/404-not-found' %} to the /404.html file. That way every 404-page gets redirected to the single. Downside: You will loose the 404-header, and the uri in the address-bar will be lost. . Load Single: Set up a single, and load the single ...


2

This doesn't work for every project, but for brochure style sites I like having all the client-arrangeable pages in one Structure, with toggles on each entry to optionally show in primary or secondary navigation, and Entry Types for different requirements such as a team page or articles section, and reserving Singles for things like homepage, privacy policy ...


2

Singles have no need for slugs, which is why we don't expose them in the CP UI for a single. They only have one in the database because they're technically entries, and entries need slugs. But it's only a concept meant for Channel and Structure entries.


2

I don't think that either the single's uri or the handle is technically considered a slug per se, although in the case of singles craft likely interprets the handle as such. A more appropriate way to access a single would be by using its section handle. (updated) {% if craft.request.getSegment(1) == "this-is-a-segment" %} {% set item = craft.entries....


2

If I understand your question correctly you might be better off using SEOmatic's Template Meta feature. Using the Template Meta you're be able to set the meta info on a per template basis. So in this case you'd create a one to use for your calendar.html template.


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