Jump to content

Only show pages in use via PageField


a-ok
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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; ?>

 

  • Like 2
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...