Forum

Thread tagged as: Question

pageDepth value in php

Is there a way of getting the pageDepth value out of the database with php so that I can set it as a variable and pass it into a perch_content_custom template?

Martin Underhill

Martin Underhill 5 points

  • 4 years ago

Basically, I've got a page with multiple item content regions and each item is being output to the page. I'm using this to display an on-page sub nav (using fragment identifiers on each item):

  perch_content_custom('Primary content', [
    'template' => 'course_nav.html',
  ]);

and the template is:

<perch:before>
  <nav class="sub-navigation">
    <ul id="menu">
</perch:before>
      <li>
        <a href="#<perch:content id="slug" type="slug" for="title" />">
          <perch:content id="title" type="smarttext" />
        </a>
      </li>
<perch:after>
      <li>
        <a href="/open-courses/half-day-courses">
          Half day courses
        </a>
      </li>
    </ul>
  </nav>
</perch:after>

My problem is that I don't want that last <li> to display on the 'half day courses' sub page. If I was using perch_pages_navigation I could wrap that last <li> in <perch:if id="pageDepth" match="eq" value="1" />content<perch:else /></perch:if>. So I need to find the value of pageDepth on the page in php, set a variable and pass that into the template and use it as the conditional.

Duncan Revell

Duncan Revell 78 points
Registered Developer

I haven't tested this, but...

I think you could use perch_pages_navigation with the from_path option set to * (to start outputting at the current page) and also set levels to 1 - so you're only outputting the detail for the current page.

Pass in a custom template that only outputs pageDepth, set the last parameter to true and pass that value into your perch_content_custom function - using PerchSystem::set_var

Hi Duncan,

Thanks for the pointer – I'm trying this:

  $a = perch_pages_navigation([
    'from-path'     => '*',
    'from-level'    => 1,
    'skip-template' => true,
  ], true);

  $sub_pages = $a['0']['pageDepth'];
  echo $sub_pages;

but it echoes out the value of the sub page on both the top level page and its sub page. I tried swapping the value for pageNavText and it echoes the name of the sub page on both pages too.

Is my code along the lines of what you were thinking?

Duncan Revell

Duncan Revell 78 points
Registered Developer

Hi Martin,

yes, that's what I was thinking. That'll teach me not to try it first...

Hang on, try this:

$a = perch_pages_navigation([
 'from-path' => '*',
 'levels' => 1,
 'skip-template' => true,
 ], true);

  $sub_pages = $a['0']['pageDepth'];
 echo $sub_pages;

Use levels not from-level

Got it! That code outputs the subpage on the top level page, but nothing on the subpage. So I did this on the page:

  $a = perch_pages_navigation([
    'from-path' => '*',
    'levels' => 1,
    'skip-template' => true,
  ], true);
  $sub_pages = $a['0']['pageNavText'];
  PerchSystem::set_var('subpage', $sub_pages);
  perch_content_custom('Primary content', [
    'template' => 'course_nav.html',
  ]);

And a <perch:if exists="subpage"> in the template.

Thanks for your help Duncan :D