Jump to content

Page reference child values


Peter Knight
 Share

Recommended Posts

I have a PageReference field called related_random where my client selects a particular parent page in the tree.

On the front end I then need to get a random child page of that page based on some selector values.

At the moment I'm using a Matrix field and this outputs the page ID (1090) on the front end 

<?php
foreach($page->related_products as $item) {
if($item->type == 'related_product') 
echo "
		{$item->related_random->title}	
	";
}
?>

What I can't get my head around (and what I really want to do to echo some values of that page IDs children 

Essentially I want to get a maximum 1 random child page of 1090 with a template of product.

The shoddy selector below is clearly wrong but correct in explaining what I want to do.

$related_random->child->getRandom("template=product,limit=1")

Don't know if that makes any sense? 

 

 

Link to comment
Share on other sites

$maxProducts = 1;
$productParent = $page->related_random;

$pages->find("parent=$productParent, template=product, sort=random, limit=$maxProducts");

or

$page->related_random->children->find("template=product, sort=random, limit=1"); 

 

Link to comment
Share on other sites

Here's what I did. I set up a Page Reference Field, where client can pick a parent.

5995cc2fb0a7e_ClipboardImage(4).jpg.fdab24cc84f53fa46954af1911baa472.jpg

This means with $page->related_random I can get the parent.

Getting its children is simply $page->related_random->children->find(). I then iterate over them and output some data

5995cd0ec2055_ClipboardImage(5).thumb.jpg.d7c55ff418e24c6f6d22043e104e6dca.jpg

 

 

  • Like 2
Link to comment
Share on other sites

20 hours ago, adrian said:

@Peter Knight - where have you been? :)

Ah - Debugger I see. I never really know what to do with it. Such a huge array of options I don't fully understand. 
Ever consider making a video walkthrough?

I solved my earlier issue. I thought I needed to make this more complex than it needed. In the end, it was just a case of

 

// For each related_products in Matrix field set a variable called $matrix_item
		foreach($page->related_products as $matrix_item) {
		
			// If the Matrix field content is of type: 'related_product'
			if($matrix_item->type == 'related_product'){


			// For every child page of the related_random page, output a single random one based on the product template...
			foreach ($matrix_item->related_random->children('template=product, sort=random, limit=1') as $prod){

			// output this
				echo "...

 

  • 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

×
×
  • Create New...