1

I have a Channel 'Articles' with an Entries field which is linked to entries in 'Staff Members' channel. This is so that articles can be assigned to different members of staff.

I have the generic listing (all articles) page working fine I just need to show the same page but filtered by a selected staff memeber. E.g. When viewing an individual article by a member of staff I would like to link to a page which only lists the articles assigned to that particular staff member. This far would be ok but if possible I would also like to get the staff member name into the url... http://mysite/staff/joe-bloggs/articles

Any suggestions?

Many thanks

1

Set up a custom route in config/routes.php so that that URI points to your existing listing template:

return [
    'staff/<route_staffMember>/articles' => ['template' => '_listings/default'],
];

Then simply use the relatedTo parameter to alter which entries get listed. The template will have access to a variable called route_staffMember which you can use to fork logic between the filtered/non-filtered views. (I like to prefix these variables with route_ because when I look at the code 6 months later I always scratch my head wondering where this magic variable is being set!)

For example:

_listings/default

{% set staffMemberId = route_staffMember is defined ? craft.entries.section('staff').slug(route_staffMember).ids() : null %}
{% set entries = craft.entries.section('articles').relatedTo(staffMemberId).all() %}

{# ...or, if you're using pagination: #}
{% paginate craft.entries.section('articles').relatedTo(staffMemberId).limit(12) as pageInfo, entries %}
2
  • Sorry, this is out of my brain range! I have set it up as you suggest but I get 'template not found'. I think I may be using the wrong method to link to the page? On the individual article page I have a link that looks like this <a href="/staff/{{ staff.title|kebab }}/articles">See more articles by {{ staff.title }}</a>. I'm setting the 'staff' variable with {% set staff = entry.linkArticleWithStaffMember.one() %} where linkArticleWithStaffMember is an entries field. – Martin Jun 8 '20 at 9:40
  • It should work ok... double-check your syntax. What is your template called that you want to route to? I named mine _listings/default - be sure to change that to whatever template you're using. – James Smith Jun 8 '20 at 13:15

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.