Forum

Thread tagged as: Question

perch_content_custom and hidden pages

I'm using the following to output data from the 'Program overview' region on a bunch of pages:

<?php
            perch_content_custom(['Program overview'], [
                'page' => '/internships/*',
                'template' => 'listings.html',
                'count' => 100,
                'sort'=>'overview_country',
                'sort-order'=>'ASC',
                'each' => function($item) {
                    $item['page_title'] = perch_page_attribute('pageTitle', array(
                    '_id' => $item['_pageID'],
                    ), true);
                return $item;
                },
            ]);
        ?>

It works but the issue is it also includes any pages which I have ticked 'hidden from main navigation'. Is there a way I can keep those pages hidden and not have them output to the page as they are now?

Stephen Turvey

Stephen Turvey 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use perch_page_attribute() to check if the page is hidden in the same way you're getting the title.

I copied and pasted that function from another forum post I found so don't actually know how to write that code myself! Any chance you could provide the example? Thanks for help so far.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I would try:

if (perch_page_attribute('pageHidden', array( '_id' => $item['_pageID'], ), true)) {
// ... this is a hidden page ...
}

Ok thanks, this is my final code and it seems to work:

<?php
            perch_content_custom(['Program overview'], [
                'page' => '/internships/*',
                'template' => 'listings.html',
                'count' => 100,
                'sort'=>'overview_country',
                'sort-order'=>'ASC',
                'each' => function($item) {
                    $item['page_title'] = perch_page_attribute('pageTitle', array(
                    '_id' => $item['_pageID'],
                    ), true);

                    if (perch_page_attribute('pageHidden', array( '_id' => $item['_pageID'], ), true)) {
                    // do nothing if page hidden
                    } else {
                    return $item; 
                    }
                },
            ]);
        ?>