Skip to main content
added 2 characters in body
Source Link
dmatthams
  • 2.5k
  • 12
  • 19

This is untested, but you could do a loop within each category to compare it and figure out if it's the end of a branch. I've probably missed something in my code but it might help as a start.

{% set catsYouWant = [] %} {% for cat in entry.yourCategoryFieldhandle.all() %} {% set possible = false %} // If category is level one it is possibly the end of branch {% if cat.level == 1 %} {% set possible == true %} {% endif %} // Compare category against others to see if is a parent, if so we can discount it {% for catCompare in entry.yourCategoryFieldhandle.all() %}   // if catCompare is child of cat then cat is not end of branch {% if catCompare.lastancestorOf() == cat) %} {% set possible = false %} {% endif %}   {% endfor %} {% if possible == true %} {% set catsYouWant = catsYouWant|merge(cat) %} {% endif %} {% endfor %} 

This is untested, but you could do a loop within each category to compare it and figure out if it's the end of a branch. I've probably missed something in my code but it might help as a start.

{% set catsYouWant = [] %} {% for cat in entry.yourCategoryFieldhandle.all() %} {% set possible = false %} // If category is level one it is possibly the end of branch {% if cat.level == 1 %} {% set possible == true %} {% endif %} // Compare category against others to see if is a parent, if so we can discount it {% for catCompare in entry.yourCategoryFieldhandle.all() %} // if catCompare is child of cat then cat is not end of branch {% if catCompare.last() == cat %} {% set possible = false %} {% endif %} {% endfor %} {% if possible == true %} {% set catsYouWant = catsYouWant|merge(cat) %} {% endif %} {% endfor %} 

This is untested, but you could do a loop within each category to compare it and figure out if it's the end of a branch. I've probably missed something in my code but it might help as a start.

{% set catsYouWant = [] %} {% for cat in entry.yourCategoryFieldhandle.all() %} {% set possible = false %} // If category is level one it is possibly the end of branch {% if cat.level == 1 %} {% set possible == true %} {% endif %} // Compare category against others to see if is a parent, if so we can discount it {% for catCompare in entry.yourCategoryFieldhandle.all() %}   // if catCompare is child of cat then cat is not end of branch {% if catCompare.ancestorOf(cat) %} {% set possible = false %} {% endif %}   {% endfor %} {% if possible == true %} {% set catsYouWant = catsYouWant|merge(cat) %} {% endif %} {% endfor %} 
Source Link
dmatthams
  • 2.5k
  • 12
  • 19

This is untested, but you could do a loop within each category to compare it and figure out if it's the end of a branch. I've probably missed something in my code but it might help as a start.

{% set catsYouWant = [] %} {% for cat in entry.yourCategoryFieldhandle.all() %} {% set possible = false %} // If category is level one it is possibly the end of branch {% if cat.level == 1 %} {% set possible == true %} {% endif %} // Compare category against others to see if is a parent, if so we can discount it {% for catCompare in entry.yourCategoryFieldhandle.all() %} // if catCompare is child of cat then cat is not end of branch {% if catCompare.last() == cat %} {% set possible = false %} {% endif %} {% endfor %} {% if possible == true %} {% set catsYouWant = catsYouWant|merge(cat) %} {% endif %} {% endfor %}