Jump to content
sarah_hue

Get title of a page contained in a field

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!

Share this post


Link to post
Share on other sites

You do get the page object right away from the field. So this is enough.

echo $page->author->title;

Thanks, you're absolutely right. I thought I tried this in first place, I've seen too much code yesterday, I suppose. :huh:

Share this post


Link to post
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. 

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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>";    
        }}
    ?>

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      This module is inspired by and similar to the Template Stubs module. The author of that module has not been active in the PW community for several years now and parts of the code for that module didn't make sense to me, so I decided to create my own module. Auto Template Stubs has only been tested with PhpStorm because that is the IDE that I use.
      Auto Template Stubs
      Automatically creates stub files for templates when fields or fieldgroups are saved.
      Stub files are useful if you are using an IDE (e.g. PhpStorm) that provides code assistance - the stub files let the IDE know what fields exist in each template and what data type each field returns. Depending on your IDE's features you get benefits such as code completion for field names as you type, type inference, inspection, documentation, etc.
      Installation
      Install the Auto Template Stubs module.
      Configuration
      You can change the class name prefix setting in the module config if you like. It's good to use a class name prefix because it reduces the chance that the class name will clash with an existing class name.
      The directory path used to store the stub files is configurable.
      There is a checkbox to manually trigger the regeneration of all stub files if needed.
      Usage
      Add a line near the top of each of your template files to tell your IDE what stub class name to associate with the $page variable within the template file. For example, with the default class name prefix you would add the following line at the top of the home.php template file:
      /** @var tpl_home $page */ Now enjoy code completion, etc, in your IDE.

      Adding data types for non-core Fieldtype modules
      The module includes the data types returned by all the core Fieldtype modules. If you want to add data types returned by one or more non-core Fieldtype modules then you can hook the AutoTemplateStubs::getReturnTypes() method. For example, in /site/ready.php:
      // Add data types for some non-core Fieldtype modules $wire->addHookAfter('AutoTemplateStubs::getReturnTypes', function(HookEvent $event) { $extra_types = [ 'FieldtypeDecimal' => 'string', 'FieldtypeLeafletMapMarker' => 'LeafletMapMarker', 'FieldtypeRepeaterMatrix' => 'RepeaterMatrixPageArray', 'FieldtypeTable' => 'TableRows', ]; $event->return = $event->return + $extra_types; }); Credits
      Inspired by and much credit to the Template Stubs module by mindplay.dk.
       
      https://github.com/Toutouwai/AutoTemplateStubs
      https://modules.processwire.com/modules/auto-template-stubs/
    • By jds43
      Hello,
      I have a search page loosely based on Skyscrapers where I'm parsing a selector with options 'beds', 'bathrooms', 'size' fields. It is working well until I select 'Any' after I've run a search. This is where no results are returned (/?beds=&bathrooms=&size=&submit=). I want it to reset and show all results.
      I hope this isn't too vague.
       
    • By brdje
      Greetings,
       
      I've created a product database which all use the template product.php and are published on the website.
      I'm trying to create an overview table with pagination of all the products, but using $pages->get does not return an object, only the object title.
      $products = $pages->get("template=product, limit=10"); This returns 10 strings in the frontend but not the object. When looping over the result set to get product fields, i get the following error:
      Notice: Trying to get property 'title' of non-object
       
      Because the product database is fairly large, i've added a pagination using the same query and this does return all the pages, but i can't click on the links that the paginator renders.
      $results = $pages->find("template=product, limit=10"); if($results->getTotal() > 10) { echo $results->renderPager(array( "nextItemLabel" => "Volgende", "previousItemLabel" => "Vorige", "currentItemClass" => 'active' )); }
      I've added the option in the template to allow page numbers. When i navigate to the link manually, i still end up seeing the 10 first products.
       
      I've tried changing several settings around, but i'm a bit stuck on how to resolve these issues. Do you have any advice what I need to adjust?
      Thanks in advance for your feedback.
       
       
    • By DV-JF
      Hi, 
      I'm using this kind of setup (https://processwire.com/blog/posts/language-access-control-and-more-special-permissions/#language-page-edit-permissions) in order to control the page edit permissions. Now I'm wondering if it's possible to hide the "none-ediable" language-tabs instead of striking them through.

      Many greets...
       
    • By Robin S
      Another little admin helper module...
      Template Field Widths
      Adds a "Field widths" field to Edit Template that allows you to quickly set the widths of inputfields in the template.

      Why?
      When setting up a new template or trying out different field layouts I find it a bit slow and tedious to have to open each field individually in a modal just to set the width. This module speeds up the process.
      Installation
      Install the Template Field Widths module.
      Config options
      You can set the default presentation of the "Field widths" field to collapsed or open. Field widths entered into the Template Field Widths inputfield are only applied if the Edit Template form is submitted with the Template Field Widths inputfield in an opened state. "Collapsed" is the recommended setting if you think you might also use core inputs for setting field widths in a template context. You can choose Name or Label as the primary identifier shown for the field. The unchosen alternative will become the title attribute shown on hover. You can choose to show the original field width next to the template context field width.  
      https://github.com/Toutouwai/TemplateFieldWidths
      https://modules.processwire.com/modules/template-field-widths/
×
×
  • Create New...