Get level of the page
Permalink 2 users found helpful
Hi,
in my theme i'd like to have a side-navigation that complements my top-navigation.
The side-navigation shall only appear, if the site is not the 'Home'-page (first level).
If it is a subpage of 'Home' on the second level, the side-navigation shall display the pages below (on the third level).
If the page is on the third level, the navigation should look exactly like on the second level (display pages on the current level).
I have written the following code, wich works for pages on the second level.
<?php
$page = Page::getCurrentPage();
$cntChilds = $page->getNumChildren();
if ($cntChilds > 0)
{
echo '<div class="sidebarblock">';
$bt = BlockType::getByHandle('autonav');
$bt->controller->displayPages = 'below';
$bt->controller->orderBy = 'display_asc';
$bt->controller->displaySubPages = 'all';
$bt->controller->displaySubPageLevels = 'all';
$bt->render('view');
echo '</div>';
}
?>
It does not work for the 'Home'-page on the first level (because the navigation is shown) and also for the pages on the third level, because there the code tries to find the children.
So how can i find out on what level i am?
in my theme i'd like to have a side-navigation that complements my top-navigation.
The side-navigation shall only appear, if the site is not the 'Home'-page (first level).
If it is a subpage of 'Home' on the second level, the side-navigation shall display the pages below (on the third level).
If the page is on the third level, the navigation should look exactly like on the second level (display pages on the current level).
I have written the following code, wich works for pages on the second level.
<?php
$page = Page::getCurrentPage();
$cntChilds = $page->getNumChildren();
if ($cntChilds > 0)
{
echo '<div class="sidebarblock">';
$bt = BlockType::getByHandle('autonav');
$bt->controller->displayPages = 'below';
$bt->controller->orderBy = 'display_asc';
$bt->controller->displaySubPages = 'all';
$bt->controller->displaySubPageLevels = 'all';
$bt->render('view');
echo '</div>';
}
?>
It does not work for the 'Home'-page on the first level (because the navigation is shown) and also for the pages on the third level, because there the code tries to find the children.
So how can i find out on what level i am?
How do you mean that? Could you please give me a code-example?
let me get this straight :-)
the thing you want, is it similar to this? :
http://cpanel1.meebox.net/~pixeldk/skovbakken/index.php/ungdom/kont...
the thing you want, is it similar to this? :
http://cpanel1.meebox.net/~pixeldk/skovbakken/index.php/ungdom/kont...
Exactly. "Ungdom" itself could also be a page where the navigation should also show the four subpages.
but this one I just added as a default for my pages, so that it appears under each level 0 page... you can add the autonav in the levels you want and if you for example only want level 2 and 3 this can also be defined without having to hardcode it into your template :-)
Sorry, i'm very new to concrete5. (;
Do you mean I have to add a template under \concrete\blocks\autonav\templates?
And how can i define that it will be shown on new pages by default?
Do you mean I have to add a template under \concrete\blocks\autonav\templates?
And how can i define that it will be shown on new pages by default?
Tried a little bit and this worked:
$root = Page::getCurrentPage(); if ($root->getCollectionID() != 1) { while ($root->getCollectionParentID() != 1) $root = Page::getByID($root->getCollectionParentID()); if ($root->getNumChildren() > 0) { echo '<div class="sidebarblock">'; $bt = BlockType::getByHandle('autonav'); $bt->controller->displayPagesCID = $root->getCollectionID(); $bt->controller->displayPages = 'custom'; $bt->controller->orderBy = 'display_asc'; $bt->controller->displaySubPages = 'all'; $bt->controller->displaySubPageLevels = 'all'; $bt->render('view');
Viewing 15 lines of 18 lines. View entire code block.
Nice work. :)
Thanks, your code is really useful.
Although I may have found a bug. I'm unable to modify page defaults on page types using this code. :/
Fresh install with concrete 5.4.2. Any suggestions to make it work?
Although I may have found a bug. I'm unable to modify page defaults on page types using this code. :/
Fresh install with concrete 5.4.2. Any suggestions to make it work?
THe problem is the while line. I commented that out and it works, but it wrecks the menus at level 3. Will investigate and post if I can find a fix.
Thanks melat0nin. Hope you can figure it out.
if you just make a custom autonav, as this already has the function getLevel() - then you can do what you wan't with your autonav block and your sitemap levels... :)