Jump to content

Cross reference page field with a child page


a-ok
 Share

Recommended Posts

I have a bunch of child pages added to a parent section called 'Projects'. These are all being looped out on an overview; for example:

<?php $projects = $pages->find('template=project-detail, sort=sort'); ?> <?php foreach ($projects as $project) : ?>

These pages will become quite long overtime, but I want to add a 'featured' option to them so they add a class to their element if they are 'featured'. Normally I would just add a tickbox so if it's ticked, it adds the class etc. However, as there are going to be so many pages, looking through them all and maintaining this (as the 'featured' option will change often) will get a bit messy. So, I've created a 'Page field' for the overview, so you can choose which pages you want as 'featured' and remove them easily. However, I'm having an issue working out how I can tell the child page that it has been 'chosen' to be featured, if you see what I mean? How can I, within the $projects loop, cross reference it with the 'featured' page field on the overview, and if they match, add a class?

For example

<?php $featured = $pages->get('template=home')->featured_items; ?> // Getting an array of the featured items
<?php $projects = $pages->find('template=project-detail, sort=sort'); ?>
<?php foreach ($projects as $project) : ?>
<div class="item <?php if ( $featured->id == $project->id) : ?>featured<?php endif; ?>"></div> // Checking if the featured id contains project->id
<?php endforeach; ?>

I know the above isn't right, but you get the idea. Can you help at all?

Link to comment
Share on other sites

Okay so based on a response to another query I had previously about exploding arrays, I did the following, which seemed to have worked:

<?php $featured = explode('|', $pages->get('template=home')->home_featured_items); ?>

Then within the loop

<?php foreach ($projects as $project) : ?>

I did

<?php if (in_array($project->id,$featured)) : ?> featured<?php endif; ?>

And it works. Any thoughts on this?

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

×
×
  • Create New...