a-ok Posted March 4, 2017 Posted March 4, 2017 Hi all. I'm using a simple parent/children page setup to act like categories (Categories > category 1, category 2, category 3 etc). When adding a new page, using the PageField, the user can multi-select which 'categories' this page relates to. Simple. On the front end I am looping through the categories: <?php $categories = $pages->find('parent=/categories/, sort=name'); ?> <?php foreach ($categories as $category) : ?> <li><a href="#" data-name="<?php echo $category->name; ?>"><?php echo $category->title; ?></a></li> <?php endforeach; ?> This is then going to act as a filter... however... I'm wondering if it's possible to only show the categories that are currently in use... rather than showing them all? What do you think?
Sergio Posted March 4, 2017 Posted March 4, 2017 You can have something like this, considering your page field is called "categories". Not tested! <?php $categories = $pages->find('parent=/categories/, sort=name'); ?> <?php foreach ($categories as $category) : if ($pages->count("categories=$category")) > 0 : ?> <li><a href="#" data-name="<?php echo $category->name; ?>"><?php echo $category->title; ?></a></li> <?php endif; endforeach; ?> 2
a-ok Posted March 4, 2017 Author Posted March 4, 2017 2 hours ago, Sérgio said: You can have something like this, considering your page field is called "categories". Not tested! <?php $categories = $pages->find('parent=/categories/, sort=name'); ?> <?php foreach ($categories as $category) : if ($pages->count("categories=$category")) > 0 : ?> <li><a href="#" data-name="<?php echo $category->name; ?>"><?php echo $category->title; ?></a></li> <?php endif; endforeach; ?> This was perfect, thanks.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now