3

I try to compose an entry URL for artists pages with first name and last name.

Now:

artists/{slug} = artists/{{ entry.title }}
{slug} = {{ entry.title }} = Artist Last Name

What I want to add at the end of the URL (Plain Text field):

{{ entry.artistFirstName }} = Artist First Name

What I try to get, but which is not working:

artists/{slug}-{{ entry.artistFirstName }}

Is it possible? how? And how can I add a conditional to check if entry.artistFirstName is defined?

Thanks!

4
  • you could just have the URL in your template any way you like ("/template/slug/firstname/lastname") and then just use the {slug} when fetching the entry from the database. An idea. – naboovalley Aug 10 '14 at 7:56
  • I don't know if I well understood, but I tried to add something like {% set slug = "/entry.artistFirstName/entry.title/" %} in the template requested to display this page but this didn't affect the URL. – Romain Poirier Aug 10 '14 at 8:16
  • I was thinking you leave the URL setting as is in Craft (like "artists/{slug}" for example). Then when creating the links in Twig just make them "/artists/{slug}/{entry.artistFirstName}". In your template when just fetch the single entry using segment #2. Could that work for you? – naboovalley Aug 11 '14 at 8:22
  • Thanks! I created a link like <a href="{{entry.artistFirstName}}-{{ entry.url }}"> that works, but of course the link points to a page that does not exist and generates an error 404. How can I just fetch the {{ entry.url }} segment and continuing to display the full URL I want? – Romain Poirier Aug 11 '14 at 18:53
5

For this a good approach is to use the excellent Dynamic Routing system in Craft.

Solution #1

First create your section "Artists":

enter image description here

After that go to "Settings" -> "Routes" and add this Dynamic Route:

enter image description here

When this is done all URL requests made to "/artists/-whatever-" will be routed to the "artists/_entry" template. Now you can create your link like this:

<a href="/artists/{{ entry.slug }}/{{ entry.firstNameField }}/{{ entry.lastNameField }}">Artist Name</a>

Your URL might now look like this rendered:

<a href="/artists/ladygaga/firstname/lastname">Lady Gaga</a>

In your "_entry" template you can now fetch your entry using this code:

{% set entry = craft.entries.section('artists').slug(craft.request.getSegment(2)).first() %}
Artist Name: {{ entry.title }}

Solution #2

If you instead want the URL to be "/artists/firstname-slug" you can alter the Dynamic Route slightly and use the Twig filter "split" to get the slug from the URL.

First change the Dynamic Route to:

artists/*

Then update your template "_entry" with the following code:

{% set segments = craft.request.getSegment(2)|split('-') %}
{% set entry = craft.entries.section('pages').slug(segments[1]).first() %}
Title: {{ entry.title }}

Your URL can now be "/artists/{{ entry.firstNameField }}-{{ entry.slug }}" and the template code will take care of the rest.

7
  • Thanks! After after few adjustments, it finally works for me. But is there a way to get an URL like /firstname-lastname instead of /firstname/lastname? I tried some Routes with - instead of / but got 404 errors or wrong pages. – Romain Poirier Aug 12 '14 at 20:11
  • Just make the route "/artists/*/*" instead and make the URL "/artists/slug/firstname-lastname" in your template. Will be the same result :) – naboovalley Aug 12 '14 at 20:19
  • As explained in my request: lastname = slug. So I'm trying an url like this: /artists/firstname-slug (to get something like /artists/lady-gaga, where gaga is the title of my entry = the slug). Unfortunately that does not work like this. – Romain Poirier Aug 12 '14 at 20:28
  • Please see my edited post @Romainpoirier on how to make a URL the way you want it. Please accept my answer if you feel it's the right one. – naboovalley Aug 12 '14 at 21:30
  • Thank you very much for your clear explanations! Now it works perfectly for me! :-) – Romain Poirier Aug 13 '14 at 6:46

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.