Jump to content

Display Menu using Selectors and Checkboxes


ptjedi
 Share

Recommended Posts

Greetings all,

I am trying to separate which pages display on the header menu and those which appear on footer menu. For that I created two checkbox fields, applied them to the templates, and now I am trying to filter them in the menu list.

Here's my code:

<?php
   [b]$homepage = $pages->get("/")->find("visibility_in_top_menu=1");[/b]
   $children = $homepage->children;
   // $children->prepend($homepage);

   $i = count($children)-1;
   foreach($children as $child) {
  $class = $child === $page->rootParent ? " class='on'" : '';
  if($i>0){echo "<li><a$class href='{$child->url}'>{$child->title}</a></li>";}
  else {echo "<li><a$class href='{$child->url}' id='last'>{$child->title}</a></li>";}
  $i-=1;
   }
  ?>

The thing is that I am probably not setting up the first line well.

Can anybody help me out?

Thanks!

Link to comment
Share on other sites

One more thing:

I understand that the following line adds the "on" class to the menu item if the current page matches that item, however I don't understand how that works.

$class = $child === $page->rootParent ? " class='on'" : '';

Could you please explain it to me?

This is because I've changed pages level like this:

$homepage = $pages->get("/en/");

and now the on states aren't working...

Thanks

  • Like 1
Link to comment
Share on other sites

I think I got it even though I really don't understand why now this is needed.

Changed

$class = $child === $page->rootParent ? " class='on'" : '';

to

$class = $child === $page ? " class='on'" : '';
Link to comment
Share on other sites

$class = $child === $page->rootParent ? " class='on'" : '';

is a short way of writing

if($child === $page->rootParent) {
$class = " class='on'";
} else {
$class = '';
}

$page->rootParent gets the ancestor that is closer to the root (a direct children of the homepage). If you change the homepage variable to a something different from the root, they will never match. For this to work you have to replace $page->rootParent for this page's ancestor that is closer to /en/.

Put something like this after the first lines:

foreach($page->parents as $v) {
if($children->has($v)) {
	$newRootParent = $v;
}
}

Then replace $page->rootParent by $newRootParent.

I'm not on my computer, and I can't test this right now, so, no guarantees here :)

  • Like 4
Link to comment
Share on other sites

  • 8 months later...

Thanks, diogo! Your post helped me to solve my (quite similar) problem. I'm still not used to the short version of this if-function - but the long version helped me understand it and to fill in the right pieces. 

Link to comment
Share on other sites

@Georgson - That is called the ternary conditional operator. 

From the manual .. 

<?php
$first ? $second : $third
?>

If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.

 
For those interested read more at php.net ... 
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...