2

I need some help with syntax on this for Craft 3, please.

I want to use this piece of code:

{% set fruitEntries =  craft.entries.relatedTo({ relatedTo: catP,id: 'and, not '~str }) %}

from this answer here:

How to get entries related to current category only (not children)

It's exactly what I need to do but I'm not sure how to code it for Craft 3 so that I can pull in entries from JUST my 'products' section:

craft.entries.section('products')...

Thanks in advance.


UPDATED INFO.

Category Group = Products

Entry Sections using these categories = Products & Case Studies

My category structure is as follows:

Categories are:

  • Travelling Irrigators
    • Superseeded Models
  • Hydrants
  • Filters
  • Pontoons
  • Pumps

I'm trying to return just the product entries (not case studies) under 'Travelling Irrigators' but not including the 'Superseeded Models'.

The other post does exactly what I need but it returns the case studies too.

  • What category are you looking to retrieve entries from? How is it linked to the product entries? – Seb Sep 30 at 21:00
1

These two pages should help you from the Craft 3 docs

https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#relatedto

https://docs.craftcms.com/v3/relations.html

  • Thanks for posting the links but I've already been on the docs and am still stuck hence my post. Im still pretty new to Craft so would appreciate someone helping with the correct syntax so I can learn from here :) – xenowebdev Sep 29 at 22:00
0

So I finally got there in the end and am posting in case it helps anyone else.

I was building a drop down menu with bootstrap that showed a 2 tier category / entry menu.

Code as follows:

<ul class="dropdown-menu" aria-labelledby="navbarDropdown">

{% set catsL1 = craft.categories().group('products').level(1).all() %}    
{% for cat in catsL1 %} 

    {% set childCats = craft.categories.descendantOf(cat) %}
    {% set catParent = craft.categories.id(cat).one() %}
    {% set dontInclude = [] %}
    {% for child in craft.categories.descendantOf(cat).all() %}
        {% set dontInclude = dontInclude|merge(craft.entries.section('products').relatedTo(child).ids) %}
    {% endfor %}
    {% set str = dontInclude|join(', not ') %}
    {% set hideEntries = craft.entries({ relatedTo: cat,id: 'and, not '~str }) %}

        {% if hideEntries|length %}

        <!-- if we have some entries in the parent category, output them in a dropdown menu -->
        <li class="dropdown-submenu dropdown">
            <a class="dropdown-item dropdown-toggle" href="#" id="navbarSubDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ cat.title }}</a>
            <ul class="dropdown-menu" aria-labelledby="navbarSubDropdown">
                {% for ent in hideEntries.section('products').all() %}
                <li><a href="{{ cat.url }}/{{ ent.slug }}" class="dropdown-item" style="color:grey">{{ ent.title }}</a></li>
                {% endfor %}

                <!-- check to see if sub categories exist -->
                {% if childCats|length %}
                    {% for subCat in childCats.all() %}

                        <!-- check to see if there are entries within the sub categories -->
                        {% set subEntries = craft.entries.section('products').relatedTo(subCat).all() %}
                        {% if subEntries|length %}
                        <li class="dropdown-submenu dropdown">
                            <a class="dropdown-item dropdown-toggle" href="#" id="navbarSubDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ subCat.title }}</a>
                            <ul class="dropdown-menu" aria-labelledby="navbarSubDropdown">
                                {% for entry in subEntries %}
                                    <li><a href="{{ cat.url }}/{{ subCat.slug }}/{{ entry.slug }}" class="dropdown-item">{{ entry.title }}</a></li>
                                {% endfor %}
                            </ul>
                        </li>
                        {% else %}
                        <!-- if no entries then just output the child categories -->
                        <li><a href="{{ subCat.url }}" class="dropdown-item">{{ entry.title }}</a></li>
                        {% endif %}


                    </li>
                    {% endfor %}
                {% endif %}
            </ul>
        </li>

        {% else %}

            <!-- check to see if there are sub category entries, if so, output them in a dropdown -->
            {% set subEntries = craft.entries.section('products').relatedTo(cat).all() %}
            {% if subEntries|length %}
            <li class="dropdown-submenu dropdown">
                <a class="dropdown-item dropdown-toggle" href="#" id="navbarSubDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ cat.title }}</a>
                <ul class="dropdown-menu" aria-labelledby="navbarSubDropdown">
                    {% for entry in subEntries %}
                        <li><a href="{{ cat.url }}/{{ entry.slug }}" class="dropdown-item">{{ entry.title }}</a></li>
                    {% endfor %}
                </ul>
            </li>
            {% else %}
            <!-- if no entries, then just show the top level categories -->
            <li><a href="{{ cat.url }}" class="dropdown-item">{{ cat.title }}</a></li>
            {% endif %}

        {% endif %}

    </li>

{% endfor %}

</ul>

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.