Jump to content

page content within another page


Marty Walker
 Share

Recommended Posts

Hi,

I had this crazy idea that I could setup a page field in my template that references a couple of unlisted pages - sort of like sidebar content. I wanted to test that the page field is populated and be able to display that pages content.

Is that even possible?

Regards

Martin

Link to comment
Share on other sites

I used to do it like this:

Add page reference field, that configured to allow to select pages with template "sidebar" for example. This sidebar template has just title and body and may other fields. Now add a page tree section ie "Sidebar Elements" in the tree, where you create the sidebar pages. Now on your normal content pages you can select 1 or more of them through the page reference field and even sort them. You can then render them using foreach over fields or using ->render method (need to install the module I think) for pages to render them using their own template file, which would only contain the markup for the sidebar element of course.

There's a few more options and ways to archive this, but this turnes out to be very flexible and easy. You could also inherit those to while sub page branches aswell, like the mood image on basic site.

  • Like 1
Link to comment
Share on other sites

Thanks Soma. That's exactly how I had it set up. It's the code that's always a mystery to me. This is (basically) what I have so far:

<?php
if($page->side_panel) {
$side = $pages->get("/side-panels/")->find("limit=1"); 
foreach($side as $s) {
echo "<p>{$side->title}</p>";
}
}
Link to comment
Share on other sites

I assume that side_panel is your page-field. Coding depends little bit if it allows multiple values or not, but here are examples for both:

Multiple:

<?php

if($page->side_panel) {
 foreach($page->side_panel as $p) {
   echo "<p>{$p->title}</p>";
 }
}

Single:

<?php
if($page->side_panel) {
 echo "<p>{$page->side_panel->title}</p>";
}
?>

Written in browser, so there might be some details wrong..

EDIT: As you can see it is much simpler than what you have tried earlier. When you learn this, then only sky is the limit with ProcessWire ;)

  • Like 1
Link to comment
Share on other sites

This is correct, but not using the page->render module method I mentioned.

Assuming you have a template setup for the side panel with a php file like this:

<div class="side_panel">
  <h4><?php echo $page->title; ?></h4>
  <?php echo $page->body; ?>
</div>

Assuming the page field is allowed multiple,

then your code could be:

<?php

if($page->side_panel) {
 foreach($page->side_panel as $p) {
   echo $p->render();
 }
}

$page->render(); just renders your side panel page using the template file with the variables filled in.

Then you can have multiple different side panel types with own templates and fields for own purposes.

Hope that makes sense.

  • Like 1
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...