Jump to content

checkbox checked condition for menu item


fruid
 Share

Recommended Posts

I'm trying to put pages in the main menu on condition that the checkbox, a field named "menu_item" that I created, is checked on that specific page.

so in the _head it says:

        foreach($children as $child) {
            if($child->id == $page->rootParent->id) {
                // this $child page is currently being viewed (or one of it's children/descendents)
                // so we highlight it as the current page in the navigation
                echo "<li class='current' aria-current='true'><span class='visually-hidden'>Current page: </span><a href='$child->url'>$child->title</a></li>";
            }
            else {
                echo "<li><a href='$child->url'>$child->title</a></li>";
            }            
        }

so I figured I add 

            elseif($page->child($menu_item) == 1) {
                echo "<li><a href='$child->url'>$child->title</a></li>";
            }

but no matter how I twist and turn it it doesn't work. 

Is is $page->child($menu_item) or $child->menu_item or $page->child->menu_item or …

thanks for help

Fred

 

Link to comment
Share on other sites

Hi there, 

first the answer depends on that if your array "children" is PageArray. If yes I believe if you want currently viewed page you must check if child->id is simalar like page->id not parent->id. 

Secondly if you want to check if checkbox is checked the right way will be:

$child->checkbox_field == 1

Also I think you know this but else if must be put before else statement :D. Lastly maybe consider using custom menu builder for this. Menu Builder. 

In addition this checking can be done before building actual markup for menu. So you can consider this solution an then you won't need else if.

$menuPages = $pages->find('menu_item=1'); //and add additional selectors for template etc., also you can use selector like this for method children()

Cheers. 

 

  • Like 2
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...