1

Client has created /exhibitors but wishes it to point to /exhibitors/partners

Can that be done using routes or should we be looking at htaccess rules?

3

If it's just a simple redirect, it'll be quicker to do it in .htaccess and not invoke PHP/Craft at all.

2
  • Hey Brad, is this also how you do it with the structure on buildwithcraft.com → Documentation → Installing and Updating?
    – carlcs
    Oct 6 '14 at 15:49
  • No it's not, just checked the urls in the code. :D
    – carlcs
    Oct 6 '14 at 15:53
2

We had a pretty similar question already: "Setting up redirects via Craft routes?".

Here's how you could change the code to exchange the parent entry with its first child conditionally. Additionally you can also add a redirect to that child page, in case someone edits the url.

{% set linkToChildEntries = ['exhibitors', 'anotherparent'] %}

<ul>
    {% nav page in pages %}
        <li>

            {% if page.slug in linkToChildEntries and page.getChildren()|length %}
                <a href="{{ page.getChildren().first().url }}">{{ page.title }}</a>
            {% else %}
                <a href="{{ page.url }}">{{ page.title }}</a>
            {% endif %}

            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}

        </li>
    {% endnav %}
</ul>

Of course you can also make this client editable by adding a field to the parent's entry type you then check for instead of managing the to be treated parent entires in the template code.

1

I don't have any of the code to hand so I'll have to check it later, but I had a similar need and wanted something I could switch on and off across multiple entries, so I went with a lightswitch field and the following code in the template:

{% if entry.firstChildRedirect %}
    {% if entry.children|length %}
        {% redirect entry.children.first.uri %}
    {% endif %}
{% endif %}

I've also got a function in a variable that when given an entry, either returns that entry, or it's first child, using similar logic.

<?php
namespace Craft;

class YourPluginVariable
{
    public function entryOrRedirected(EntryModel $entry)
    {
        if($entry->firstChildRedirect)
        {
            $child = $entry->children->first();

            if($child) return $child;
        }

        return $entry;
    }
}

Which can be used in your templates:

{{ craft.yourPlugin.entryOrRedirected(entry).getLink }}

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.