Using $page->isDescendantOf on a subpage

Im trying to use $page->isDescendantOfon a subpage, but it throws a whoops. It works if i give it a top level page, but i want to test if the current page is a descendant of a particular subpage. Is this expected behaviour? How can I do it?

How are you using it? Please post your code and the page structure

Has this been resolved, @jimbobrjames?

Ah… i solved it another way, sorry I meant to come back and say. But i never did get $page->isDescendantOf to work from a child down rather than a top most level page. Think i ended up matching from an array of ID’s with the help of AutoID instead.

I’d still be interested in what you tried that didn’t work?

Well ok… if you promise not to raise your eyebrows at how i did this… :slight_smile:

the code has evolved a bit since i asked but it was something like this…

<?php $thispageid = $page->autoid()->value() ?>

<?php if($page->isDescendantOf('toplevelone') || $thispageid == '65'): ?>
  <?php snippet('navs/nav-subpagelist', array('pagelist' => ['65', '159', '66', '68', '71', '72', '73' ])) ?>
<?php elseif($page->isDescendantOf('topleveltwo') || $thispageid == '58'): ?>
  <?php snippet('navs/nav-subpagelist', array('pagelist' => ['58', '160', '74', '78', '79', '76', '75' ])) ?>
<?php elseif($page->isDescendantOf('toplevelthree') || $thispageid == '29'): ?>
  <?php snippet('navs/nav-subpagelist', array('pagelist' => ['125', '127', '128', '1' ])) ?>
<?php elseif($page->isDescendantOf('toplevelfour') || $thispageid == '124'): ?>
  <?php snippet('navs/nav-subpagelist', array('pagelist' => ['125', '127', '128', '1' ])) ?>
<?php else: ?>
  <!-- no match -->
<?php endif ?>

nav-subpagelist snippet just lists out the array of IDs in the order supplied.

<?php $relevantPages = $site->index()->visible()->filterBy('autoid', 'in', $pagelist); ?>
  <?php foreach($pagelist as $id): ?>
    <?php if($p = $relevantPages->findBy('autoid', $id)):?>
    <li id="<?= $p->uid() ?>-menu"><a <?php e($p->isOpen(), ' class="active"') ?> href="<?= $p->url() ?>"><?= $p->title() ?></a></li>
    <?php endif ?>
  <?php endforeach ?>

basically if changed the descendentOf in any of these to any page that had children, but wasn’t at the very top level, it broke.

<?php if($page->isDescendantOf('subpageone') || $thispageid == '112'): ?>

That’s because the method wants either the URI or a page object as parameter, not just the page id.

I see…thanks…i’ll read that again tomorrow when i haven’t had three :beers: :slight_smile:

Lucky you, my fridge is as empty as can be.

Oh dear. Well, i have one left I would gladly give you if you weren’t so far away. Would you care to explain again in terms a slightly tipsy person can understand? :slight_smile:

I appreciate it :heart_eyes:

Well, for example, pass the URI to. the page, which is the complete path:

<?php if($page->isDescendantOf('mainlevel-page/subpageone') || $thispageid == '112'): ?>

Or passing a page object as parameter:

$subpage = page('some/page/really/deep/down/the/tree');
if($page->isDescendantOf($subpage) || $thispageid == '112'): ?>
1 Like