TransWikia.com

Can I use a structure without slugs?

Craft CMS Asked by Arne Van Kauter on June 3, 2021

I’m making a portfolio website, where the url’s look like “projects/project-name”.
There is also an overview page with all the projects that is under “projects” on itself.

I’m using a Structure for this, but I go to the 404 page when I’m just trying to go to “projects” without the slugs.

Is there a way of using a structure without a slug?

One Answer

You'll need to create a listing template for the base /projects URL, and make that template render for that URL.

The easiest way is to just add a template called /templates/projects/index.html, in which case Craft will automatically render that template whenever a user hits example.com/projects.

Another way is to create a dynamic route, and point it to whatever template you want rendered for the /projects URL.

Obviously, inside your listing template you'll need to pull the Structure entries manually, i.e. by doing something like this:

{% for entry in craft.entries.section('projects') %}
    <article>
        <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>
        ...
    </article>
{% endfor %}

Note that if you don't pass an .order() parameter to your craft.entries query, Craft will use the default order, which is the "manual" structure order for Structure sections.

Also note that if your Structure is multi-level, you'll probably want to use the {% nav %} tag over the basic {% for %} loop:

{% nav entry in craft.entries.section('projects') %}
    <li>
        <a href="{{ entry.url }}">{{ entry.title }}</a>
        {% ifchildren %}
            <ul>
                {% children %}
            </ul>
        {% endifchildren %}
    </li>
{% endnav %}

Correct answer by Mats Mikkel Rummelhoff on June 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP