Jump to content

Cross reference page field with a child page

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?

Share this post

Link to post
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?

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.


      Step 2: Select the images with the trashcan symbol

      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By stanoliver
      In the new page-meta-method (https://processwire.com/blog/posts/pw-3.0.133/#comments) there is an example how to ouput "world". 
      Could may someone give me a snippet of code so that the output is one of the colors (red, green, blue).
    • By VeiJari
      Hello forum. I'm trying to figure out how to detect if current form has errors after saving it in pw admin page. This is so that our event location isn't added in database if we detect errors. But I can't seem to find a correct way to detect errors? I only found the wire()->errors() and it always triggers even if I don't have any errors. 
      How can I detect errors in a form?
      By error I mean $page->error('this is the red error');
  • Create New...