How to find parent page name on search result

Permalink
Hello Concrete5 Masters,

I need a little help. I am creating a custom template on search block. What I want is show parent page name of the search result.

Looking at the search block view file, there is this:

<?php  foreach($results as $r) { 
//Search result page names and urls etc here; $r->getPath(), $r->getName() etc
}


Is there a way where I could do something like:

if($r->parentPageName()=="parent_page_name") {
// Do some stuff here
}


Any hint/direction will be highly appreciated.

sangay
 
VPenkov replied on at Permalink Reply
VPenkov
First, get the result's ID:
$childId= $cobj->getCollectionID();


THEN work with its parent.
$parentId=Page::getByID($childId->getCollectionParentID()); //notice how I use $childId
$parentName = $parentId->getCollectionName();
echo $parentName; //just to see if it's correct


Not tested but should work.
Put both code snippets inside your foreach cycle. Then you can do if($parentName == "blabla") ...

That's the whole c5 logic and most stuff is done this way.
MathiasB replied on at Permalink Reply
MathiasB
Hi MoonGrab!
I tryed for hours to insert/edit your code in the view.php but it doesn't work.
Why is it possible to display the parent page "ID" but not it's name???

Best Mathias
MathiasB replied on at Permalink Reply
MathiasB
Dear friend,
finally I found a way to display the parents page name in my search results.
Put this anywhere you want:

<?php
$parent = page::getByID($page->getCollectionParentID());
print $parent->getCollectionName();
?>

Best Mathias