Jump to content

Excluding child pages from menu


Dean
 Share

Recommended Posts

Hi, I currently have this code:

$navigation = $page->rootParent->navigation

 

if(count($navigation) > 0)

{

foreach($navigation as $p)

{

$children = $p->children;

 

if(count($children) > 0)

{

// accordion header

}

else

{

// simple link

}

}

}

 

for outputting my navigation links, which works fine. But, for one section (news), I don't want the subpages (articles) to appear in the navigation. I have edited the code so that

if(count($children) > 0)

becomes

if(count($children) > 0 && $p->title != 'News')

so that news articles are excluded. But I think this is not a good way to do it. Can anyone share the normal/best way to exclude pages like this?

Link to comment
Share on other sites

There are several options:

Check if the $p has a specific template (or any other field).

if(count($children) > 0 && $p->is("template!=news-item|other|template"))

Or exclude above the loop like:

$children = $p->children->not("template=news-item|other|template");

Or filter above the loop like:

$children = $p->children->filter("template!=news-item|other|template");

See the excellent cheatsheet for more options in the PageArray/WireArray. Another option might be to use to use soma's navigation module.

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