Jump to content

Find child pages with $page->children but prohibit access via their URL


bytesource
 Share

Recommended Posts

Hi Everyone,

I have a page with the following template:

product.php

<h1><?php echo $page->title; ?></h1>
<p><?php echo $page->introduction; ?></p>

<?php
$children = $page->children;
if(count($children)) {
foreach($children as $item) {
echo $item->render(); // output template associated with item page
}
}
?>

<br style='clear: both;' />

that renders one or more child pages:

item.php

<h2><?php echo $page->title; ?></h2>
<p><?php echo $page->introduction; ?></p>

These child pages are not supposed to be accessed by their URL, as they only provide content and markup to the parent page. Setting them to hidden does not work, because then they won't be found as children anymore.

I could just use a no_file template and then add the markup for the child pages directly to the parent's template file (product.php), but I prefer to keep this markup in a separate file.

Is there a way to solve this problem? Any suggestions are highly welcome!

Link to comment
Share on other sites

IF you set them to hidden you could do:

<h1><?php echo $page->title; ?></h1>
<p><?php echo $page->introduction; ?></p>
<?php
$children = $page->children('include=all');
if(count($children)) {
foreach($children as $item) {
if ($item->isHidden()) {
echo "I'm not displayed.";
} else {
echo $item->render(); // output template associated with item page
}
}
}
?>
<br style='clear: both;' />

(ps, written in browser not tested)

  • Like 1
Link to comment
Share on other sites

Hi Martijn,

Thanks a lot for your help!

Your suggestion reminded me to take a closer look at the Selectors API. There I found 'include=hidden', which might be an even better fit for my use case. Using this selector, unpublished pages won't be included.

By the way, do you if there is a way to set all pages that are attached to a certain template to be hidden (or any other status) by default?

Link to comment
Share on other sites

By the way, do you if there is a way to set all pages that are attached to a certain template to be hidden (or any other status) by default?

There isn't a setting to do that, but you can accomplish it pretty easily from a selector:

$page->children("template!=some_template"); 

$pages->find("template!=some_template|some_other_template"); 

Or, even better, specify the template you want:

$pages->find("template=good_template"); 
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...