Jump to content

Get child page title from parent id


Tony Carnell
 Share

Recommended Posts

Hi all,

Real newbie question here; how do you get a child page's title from its parent's ID?

Let me elucidate.

I have a page type of Dam with 600+ dam child pages. Each child dam page uses a template called dam, and one field within the dam template is country, which is linked to a Countries page type.

I've written code to return all the dams in a certain country, but it returns that country's ID as it appears in the dam template's country field.

Having attempted and failed miserably to get the title of the appropriate country's child page's title from the ID, I thought I'd ask those far more knowledgeable than myself!

I appreciate this is probably day one, lesson one of how to work with Processwire, but I'm still negotiating the learning curve  :rolleyes:

Tony.

Link to comment
Share on other sites

Adrian was just quicker... we probably need more code to help if that snippet doesn't work.

Anyways, I always have a good laugh when reading about those dam(n) pages and templates :)

True enough - it depends on how you are looping through the child pages. Perhaps something like this:

foreach($page->children() as $dam) {
    echo $dam->country->title;
}
  • Like 1
Link to comment
Share on other sites

True enough - it depends on how you are looping through the child pages. Perhaps something like this:

foreach($page->children() as $dam) {
    echo $dam->country->title;
}

Thanks Adrian, that worked a treat  :rolleyes:

Anyways, I always have a good laugh when reading about those dam(n) pages and templates :)

I just wish I was having such a laugh building the dam(n) site!  :P

Once again I have to thank the community for its help. It's much appreciated.

Tony.

  • Like 1
Link to comment
Share on other sites

  • 2 years later...

For some reason here I can't manage to see the title of the 'tool' $lendoutpage->tool->title, while $lendoutpage->tool shows the id.

	$lendoutpages = $pages->find("template=lendout, student_name=$page->id, sort=-lendout_time");	
	foreach($lendoutpages as $lendoutpage) 
	{	
	echo "<li><a href='{$lendoutpage->url}'>{$lendoutpage->lendout_time}: <b>{$lendoutpage->tool}/{$lendoutpage->tool->title}</b></a></li>";
	}; 

'tool' is a page reference field on the lendout page with a list of tool pages (template=tools and parent=tools).

Edited by BFD Calendar
page field = page reference field
Link to comment
Share on other sites

I'm sure some people will pull out a few hairs, but it works...

	$lendoutpages = $pages->find("template=lendout, student_name=$page->id, sort=-lendout_time");
	if("$lendoutpages, count > 0") {
	echo "<br><hr><span class='verdana'><b>Lendouts (only on English page!):</b></span><br>";
	};
	foreach($lendoutpages as $lendoutpage) 
	{
	if(empty("$lendoutpage->return_time")) {
	$return="<font color='red'><b>missing</b></font>";
	}
	else {
	$return="<font color='green'><b>return ok</b></font>";
	};
	echo "<li><a href='{$lendoutpage->url}'><span class='verdana'>{$lendoutpage->lendout_time}:<b>";
	$toolpages = $pages->find("template=tools, id=$lendoutpage->tool");
	foreach($toolpages as $toolpage) 
	{
	echo " | $toolpage->title";
	};
	echo "</b> -> {$return}</span></a></li>";
	}; 

 

Link to comment
Share on other sites

Don't you need to loop that page reference field (from your original example) if it's a list of tool pages? i.e.

$lendoutpages = $pages->find("template=lendout, student_name=$page->id, sort=-lendout_time");	
foreach($lendoutpages as $lendoutpage) {	
    echo "<li><a href='{$lendoutpage->url}'>{$lendoutpage->lendout_time}:</a>"; 
	
    foreach ($lendoutpage->tool as $single_tool_page) {
      echo "<b>{$single_tool_page->title}</b>";
    }
    echo "</li>"; 
}; 

I'm not sure you need to use page->find() twice (as in your reworked example). Something like the above.

This post may be useful:

 

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...