Jump to content
Stefanowitsch

Get page with a specific template of root parent

Recommended Posts

Hello!

This might be an easy one but I can't figure out the selector. My head is spinning.

I am using the Multisite plugin and want to display custom footer menus based on multiple "submenu" templates.

What I want to achieve:

Get the page with the template "menu_submenus" that is located under the root page of the current page.

My attempt does not work:

$menus = $page->rootParent()->get('template=menu_submenus');

    $footerNav = $menus->footer_menu;

    foreach ($footerNav as $item) { ?>

        <li><a href="<?= $item->url; ?>"><?= $item->title; ?></a></li>

For visualisation heres the page tree:

Bildschirmfoto 2019-07-20 um 11.12.53.png

Share this post


Link to post
Share on other sites

Did you debug it with Tracy?

Check whether you need parent or rootParent, and your template and field names.

Share this post


Link to post
Share on other sites

I got it! Forgot to include HIDDEN pages in the query. Typically my menu templates are hidden pages.

So this is the correct solution:

$page->rootParent()->children('include=hidden')->get('template=menu_submenus');

 

Share this post


Link to post
Share on other sites
13 hours ago, Stefanowitsch said:

So this is the correct solution:


$page->rootParent()->children('include=hidden')->get('template=menu_submenus');

When you do this you load all the children of the of the root parent into memory as a PageArray, and then you get just one of those pages. It's more efficient to directly get the single page you need:

$page->rootParent()->child('template=menu_submenus, include=hidden');

 

  • Like 1

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.

×
×
  • Create New...