2

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?

2

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 %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.