a-ok Posted July 18, 2015 Share Posted July 18, 2015 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 More sharing options...
a-ok Posted July 18, 2015 Author Share Posted July 18, 2015 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 More sharing options...
tpr Posted July 18, 2015 Share Posted July 18, 2015 The second seems fine to me, but you can use a simpler syntax (from 2.5.27): $featuredIDs = $pages->get('template=home')->featured_items->id(); http://processwire.com/blog/posts/processwire-core-updates-2.5.27/ Then <?php if (in_array($project->id,$featuredIDs)) : ?> featured<?php endif; ?> 1 Link to comment Share on other sites More sharing options...
diogo Posted July 18, 2015 Share Posted July 18, 2015 You can use a pageArray method instead of exploding things: if ( $pages->get('template=home')->featured_items->has($project) ) I actually showed this to you not that long ago https://processwire.com/talk/topic/10440-checking-array-for-a-value-might-be-more-php-question-than-pw-sorry/?p=98786 2 Link to comment Share on other sites More sharing options...
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