Jump to content

Get title of a page contained in a field


sarah_hue
 Share

Recommended Posts

Hi,

well, here's something I just don't understand and didn't find an explanation in the reference for.

I've got a number of pages:

  • Home
    • Author
      • Tomas Tranströmer
      • José Saramago
      • Kenzaburō Ōe
      • ...
    • Nobel prize
      • 2012
      • 2011
      • ...

I created a field "author", type Page, which contains all these 3rd level pages (Tomas Tranströmer, José Saramago, Kenzaburō Ōe, ...). In the template used i.e. for /nobel-prize/2011, the "author" field is populated and contains the page ("Tomas Tranströmer").

Now I would like to create an output:

$content .= $pages->get($page->author)->title;

Confusingly, the output is

Home

although the output for

$content .= $page->author;

is

1026

and the output for

$content .= $pages->get(1026)->title;

is the desired

Tomas Tranströmer

So I seem to create a problem with using $pages and $page in the same line, but what is it exactly and how will I fix it?

Thanks a lot!

Link to comment
Share on other sites

Thank you for the above. Unfortunately I cannot get it to work in my scenario where I have a foreach loop:

<?php 
$children = $page->children;
?>
<div class="listings">
<?php
foreach ($children as $child) {
?>
<div>
 <p><?=$child->headline?></p>
 <p><?=$child->property_display_price?></p>
 <p><?=$child->fields->property_category->label?>:  <?=$child->property_category->title?></p>
</div>
<?php 
}?>
</div>		

The only way I could get the 'property_category' label to display on the front end was as above specifying the 'fields' object and the property_category title is blank.

The property_category title value gets called from another page from a 'page' dropdown. Like sarah_hue, I've tried lots of variations and the only way I can get the remote page title to display on the front end is to hard code the remote page id:

<?=$pages->get('1088')->title?> 

Where 1088 is the selected value of the dropdown.

All help most welcome. 

Link to comment
Share on other sites

Hi soma

Not sure what you mean. This is my first PW site. I'm converting an existing site from another CMS.

'property_category' is a dropdown (single select). The $child page also contains other fields that are checkbox groups using the 'page' field type but haven't got to those yet.

Does this make my question clearer?

Link to comment
Share on other sites

On a page field you can configure if it's multiple or single value. If it's multiple it will be a page array and your method doesn't work cause you need to specify which in the array you want the tytle from. if you only need single value you also need to make the field single value.

Link to comment
Share on other sites

Woohoo! Got it. Thank you Soma. So much to learn!

Solution: do as you say above and make the field Single page (Page) or empty page (NullPage) when none selected, then in the template:

<p class="type"><?=$child->fields->property_category->label?>:  <?=$child->property_category->title?></p>

Still had to put $child->fields->property_category->label but all part of the learning process.

Cheers

  • Like 1
Link to comment
Share on other sites

  • 2 years later...

Hi,

Running into some issues with this myself - I have multiple pages referenced and want to output the title of these pages - works when I have a single page option but either getting the ID's (i.e. 1144|1089) or nothing when I try to call title... can someone help me? Sorry, have looked around a lot but just not getting the concept!

Thank you very much.

  <?php 
   if ($page->children) {
            foreach($page->children as $selectedthumbs) {    
                echo "<tr><th><a href='{$selectedthumbs->url}'>{$selectedthumbs->title}</th><th>{$selectedthumbs->contributors}</th></</tr>";    
        }}
    ?>

 

Link to comment
Share on other sites

Hello @Jon E

11 hours ago, Jon E said:

I have multiple pages referenced

However, you use $page->children which is the reference to "children" in the context of the Page Tree. When dealing with page reference fields of templates, you need to reference the page reference field by its name, something like $page->my_tags for example:

<?php namespace ProcessWire;
     foreach($page->selectedthumbs as $selectedthumb) {
       echo "<tr><th><a href='{$selectedthumb->url}'>{$selectedthumb->title}</a></th><th>{$selectedthumb->contributors}</th></</tr>";
}

BTW:

  • You  do not need the if clause in this case, because if your PageArray is empty, the loop will not run in the first place.
  • You left out the closing tag of <a> element
11 hours ago, Jon E said:

getting the ID's (i.e. 1144|1089)

If you are getting Page IDs then you are probably on the right track as that means you have a PageArray there. In ProcessWire the __toString() magic method renders the IDs when you access them as if they were strings. Meaning when you see them, you need to loop through the variable in question instead of echoing it.

https://github.com/processwire/processwire/blob/57b297fd1d828961b20ef29782012f75957d6886/wire/core/PageArray.php#L579

Hope this helps. If not, please provide more code and info about the context you are having trouble with.

Edited by szabesz
typo
  • Like 2
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...