3

I need to be able to display the Category Title of a single category (as a h1 tag for a page displaying a list of the entries for that category). I can successfully display the relevant entries for the category on the page, but when I have tried to display the category title it displays all of the categories titles (in a loop), not just the single category title.

My code:

{% if craft.request.getParam('category') is not null %}    

        {% set category = craft.categories.slug(craft.request.getParam('category')) %}

        {% set products =  craft.entries.section('products').relatedTo(category).find() %}


        {% else %}
               {% set products = craft.entries.section('products').find() %}
        {% endif %}



<div class="top-title"> 

     {% for category in craft.categories.group('productType').find() %}       
        <header class="flush"><h1>{{ category.title }}</h1></header> 
     {% endfor %}

Any help would be very much appreciated! Many thanks!

4

It depends how you're fetching the category. But if you've defined the template when setting up the categories it would be called be within a var called 'category', which would be a category object. You should then be able to access it using:

{{ category.title }}

If, on the other hand, you were getting the category via a slug in the URL, you would do something like:

{% set category = craft.categories.group('productType').slug(craft.request.lastSegment).first %}
{{ category.title }}

UPDATE: Following on from your updated question, you've already got the category as an object so there is no need to get it again. Try replacing:

// THIS IS YOUR LOOP, BTW
{% for category in craft.categories.group('productType').find() %}       
    <header class="flush"><h1>{{ category.title }}</h1></header> 
{% endfor %}

with:

{{ category.title }}

But you might need to add a conditional in case the page is loaded without a category. Something like:

{% if category | length %}
    {{ category.title }}
{% endif %}
  • Many thanks for your response Clive - much appreciated. Still struggling to get this to work....have amended code in original question (sorry - should have included this in my original post) :( – MGX Mar 14 '17 at 9:58
  • Many thanks for your response Clive - much appreciated. I tried both those alternatives - but unfortunately, they just output nothing/blank for the Category Title. – MGX Mar 14 '17 at 16:20
  • What does {{ craft.request.getParam('category') }} output? And didn't you have .first on the end of your initial craft.categories query before? – Clive Portman Mar 15 '17 at 10:01
  • I added that extra bit early today as it's the solution - apologies if I didn't make that clear! – MGX Mar 15 '17 at 13:25
  • Cool. You should accept MGX's answer below, as it better answers the question in it's current form. – Clive Portman Mar 16 '17 at 8:59
2

This is the solution:

{% set category = craft.categories.slug(craft.request.getParam('category')).first() %}
  • This seems to only take the first category page. How would I get the category that is related to the current page I'm on, like, If this page has a category related to it, display that category title and link? Thanks @BradBell – mateostabio Mar 17 '18 at 6:39

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.