sarah_hue Posted February 8, 2015 Posted February 8, 2015 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: HomeAuthorTomas Tranströmer José Saramago Kenzaburō Ōe ... Nobel prize2012 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!
LostKobrakai Posted February 8, 2015 Posted February 8, 2015 You do get the page object right away from the field. So this is enough. echo $page->author->title; 2
sarah_hue Posted February 8, 2015 Author Posted February 8, 2015 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.
psy Posted February 10, 2015 Posted February 10, 2015 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.
Soma Posted February 10, 2015 Posted February 10, 2015 Is property_category a single reference or multiple?
psy Posted February 10, 2015 Posted February 10, 2015 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?
Soma Posted February 10, 2015 Posted February 10, 2015 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.
psy Posted February 10, 2015 Posted February 10, 2015 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 1
Jon E Posted September 23, 2017 Posted September 23, 2017 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>"; }} ?>
szabesz Posted September 23, 2017 Posted September 23, 2017 (edited) 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 September 23, 2017 by szabesz typo 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now