Jump to content

Get title of a page contained in a field


Go to solution Solved by LostKobrakai,

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

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. 

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?

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.

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
Link to post
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 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
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By sambadave
      Hi there
      Short version of question
      Let's say I have a page in the admin that contains a field... Is it possible to output the content from that field on another page in the admin? Almost like a reference.
      Longer version of question (with example)
      A house builder with multiple (60+) developments. They want to be able to create notices/messages that can be added to one or many developments. Handy for things like regional covid lockdowns or temporary office closures due to bad weather.
      My approach for the admin editing options:
      Add each message to each development
      Pros: You edit the message on the development page in context
      Cons: Very time consuming and repetitive if the same message needs to be applied to 60+ developments
        Control all the messages from one admin page and say which development each message should be applied to
      Pros: Easier to add/remove messages to more than one development at a time. Control all messages from one place.
      Cons: Content is not added on development page, which is where typical admin users may expect to find it I went for option 2 due to flexibility, and created a page within the admin for global development notices. This contains a repeater with:
      Field for message to display Checkbox list of all developments. The user can select which ones to apply each message to It's working really well but the only thing is that if the user goes to a specific development in the admin, the relevant messages aren't displayed in context (as they aren't edited on that page and instead on the global development notices page)... which may cause confusion when a new staff member / content admin tries to edit the text but there is no field when they go to the development admin page where they expect to see it...
      Solution???
      I don't require the message(s) to also be editable on the development page, but I wondered if there was a nice way to show it/them somehow. I feel like I am missing something really simple as I'm sure ProcessWire will have a nice way of achieving this, or maybe there are field settings that allow this kind of thing to happen?
      Any ideas on approaches or similar experiences would be much appreciated, even if it is just a much simpler example with the content from one field being shown on another admin page to get the ball rolling.
      Thanks in advance for any advice :)
    • By rjgamer
      Hi,
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
      Thanks.
       
    • By rjgamer
      Hi guys,
      the field "redirect_last" of type DateTime got not updated. The update on the field "redirect_counter" works and got saved.
      Does anybody know what I did wrong in my code?
      if ($input->urlSegment(1) === 'redirect') { $page->of(false); $page->redirect_last = time(); $page->redirect_counter += 1; if ($page->save('redirect_counter')) { $session->redirect($page->website_url, 302); } } Thanks.
    • By theoretic
      Hi there! And thanks for Processwire!
      I have an interesting task which i cannot fulfill as i want. Maybe someone could help me please?
      Let's imagine a simple page structure of this kind:
      Category 1
      + Item 1.1
      + Item 1.2
      Category 2
      + Item 2.1
      + Item 2.2
      My task is to attach some items to more than one category, at least to show some items on different frontend category pages. With PW, it's a piece of cake. I've just created a field called Items (of type Page Reference) and attrached it to Category template. Since i have lots of items inside each category i preferred to use Page Autocomplete input for my Items field. The pages available for autocomplete are restricted by a very simple selector:
      template=item
      It works like a charm. But later i decided to make this autocomplete even smarter and to exclude current category children items from it. I tried to update my selector this way...
      template=item,parent!=(page)
      ...and oops, this broke my selector. My autocomplete founds nothing. Sorry, i had to replace the square braces by () because of this forum limitations, i swear i'm using square brackets in real-life selector!
      What am i doing wrong? And is there any way to include current page info in autocomplete-related selectors? Thanks in advance!
       
    • By daniel_puehringer
      Hi community, thanks for the great work you do 🙂

      I´m looking for a global field which can be used in the footer and has only one instance for all pages. So I´m kind of looking for "One field to rule them all".
       
      Reason for my question: The website I´m currently building has one footer for all pages. The content of the footer should be dynamic - therefore I made a global field which is automatically inserted into each page.
      Problem: if I want to change something in the footer, I have to do it for all fields, because each field has it´s own instance.

      Question: is it possible to have one global field in which changes the content for the footer of all pages?

      In most CMS Systems a simple solution to this is often impossible, I hope that processwire is different 🙂

      All the best,
      Daniel
       
×
×
  • Create New...