Boost Posted September 19, 2023 Share Posted September 19, 2023 Hei! I'm facing an issue with comparing the id property of PageArray objects in my ProcessWire template. I've been working on a blog post template, and I'm trying to display the author's name by comparing the id of the author stored in the blog post page with a list of authors. Here's the relevant code snippet from my template: // Grab other pages' content $authors = $pages->find("template=author"); <?php foreach ($authors as $item) : ?> <?php if ($item->id == $page->author->id) : ?> <?= $item->title ?> <?php endif ?> <?php endforeach ?> However, this code isn't producing any output, even though I've verified that both $authors and $page->author contain the expected id values through var_dump. object(ProcessWire\PageArray)#232 (3) { ["count"]=> int(2) ["items"]=> array(2) { ["Page:0"]=> array(5) { ["id"]=> int(1117) ["name"]=> string(14) "boost-ai-staff" ["parent"]=> string(9) "/authors/" ["template"]=> string(6) "author" ["title"]=> string(8) "Boost.ai" } ["Page:1"]=> array(5) { ["id"]=> int(1120) ["name"]=> string(12) "bill-schwaab" ["parent"]=> string(9) "/authors/" ["template"]=> string(6) "author" ["title"]=> string(12) "Bill Schwaab" } } ["selectors"]=> string(15) "template=author" } object(ProcessWire\PageArray)#244 (3) { ["count"]=> int(1) ["items"]=> array(1) { ["Page:0"]=> array(5) { ["id"]=> int(1117) ["name"]=> string(14) "boost-ai-staff" ["parent"]=> string(9) "/authors/" ["template"]=> string(6) "author" ["title"]=> string(8) "Boost.ai" } } ["selectors"]=> string(0) "" } object(ProcessWire\PageArray)#232 (3) { ["count"]=> int(2) ["items"]=> array(2) { ["Page:0"]=> array(5) { ["id"]=> int(1117) ["name"]=> string(14) "boost-ai-staff" ["parent"]=> string(9) "/authors/" ["template"]=> string(6) "author" ["title"]=> string(8) "Boost.ai" } ["Page:1"]=> array(5) { ["id"]=> int(1120) ["name"]=> string(12) "bill-schwaab" ["parent"]=> string(9) "/authors/" ["template"]=> string(6) "author" ["title"]=> string(12) "Bill Schwaab" } } ["selectors"]=> string(15) "template=author" } object(ProcessWire\PageArray)#244 (3) { ["count"]=> int(1) ["items"]=> array(1) { ["Page:0"]=> array(5) { ["id"]=> int(1117) ["name"]=> string(14) "boost-ai-staff" ["parent"]=> string(9) "/authors/" ["template"]=> string(6) "author" ["title"]=> string(8) "Boost.ai" } } ["selectors"]=> string(0) "" } I'd appreciate any insights or suggestions on how to fix this issue. Thanks! Link to comment Share on other sites More sharing options...
WillyC Posted September 19, 2023 Share Posted September 19, 2023 you showd 4 pageArrays.but what is value $page->author->id ? 1117, 1120 or mebe some thing else ? or go back .-.-.- why load.all author when u only.need 1 ? <?= $page->author->title ?> @Boost Link to comment Share on other sites More sharing options...
Gideon So Posted September 19, 2023 Share Posted September 19, 2023 Hi @Boost What is the setting of the author field in the current psge. I suppose it is a page reference field. How do you set up the field? Single select or asm select? Gideon Link to comment Share on other sites More sharing options...
Boost Posted September 19, 2023 Author Share Posted September 19, 2023 58 minutes ago, Gideon So said: Hi @Boost What is the setting of the author field in the current psge. I suppose it is a page reference field. How do you set up the field? Single select or asm select? Gideon It's a page reference, field. It's asm select Link to comment Share on other sites More sharing options...
Boost Posted September 19, 2023 Author Share Posted September 19, 2023 4 hours ago, WillyC said: you showd 4 pageArrays.but what is value $page->author->id ? 1117, 1120 or mebe some thing else ? or go back .-.-.- why load.all author when u only.need 1 ? <?= $page->author->title ?> @Boost Ok. Here is my entire setup: 1) I created an Authors page to hold the authors. Here is the author template: 2) I also created a author field that is a page reference type. 3) So, I have my author field in my blog-post template. 4) And in my blog post page, I can add one or more authors. Link to comment Share on other sites More sharing options...
monollonom Posted September 19, 2023 Share Posted September 19, 2023 You could use this instead: if($page->author->has($item)) // you could name the field "authors" to remove ambiguity The issue is that you were trying to compare with a (non-existing) id from a PageArray instead of a single author (since you allow multiple authors to be added in your page reference field) 1 Link to comment Share on other sites More sharing options...
Boost Posted September 19, 2023 Author Share Posted September 19, 2023 3 minutes ago, monollonom said: You could use this instead: if($page->author->has($item)) // you could name the field "authors" to remove ambiguity The issue is that you were trying to compare with a (non-existing) id from a PageArray instead of a single author (since you allow multiple authors to be added in your page reference field) Sorry, I'm not following you. Link to comment Share on other sites More sharing options...
monollonom Posted September 19, 2023 Share Posted September 19, 2023 3 hours ago, Boost said: 4) And in my blog post page, I can add one or more authors. You said your page reference field “author” can hold multiple authors, so when trying to call $page->author you will get an array of authors instead of a single author and so when trying to get the “id” property you won’t get any since it’s an array, which is why your code doesn’t work. So in your initial code sample, I was suggesting to replace line 5 with $page->author->has($item), like so: // Grab other pages' content $authors = $pages->find("template=author"); <?php foreach ($authors as $item) : ?> <?php if ($page->author->has($item)) : ?> <?= $item->title ?> <?php endif ?> <?php endforeach ?> 1 Link to comment Share on other sites More sharing options...
Boost Posted September 19, 2023 Author Share Posted September 19, 2023 2 minutes ago, monollonom said: You said your page reference field “author” can hold multiple authors, so when trying to call $page->author you will get an array of authors instead of a single author and so when trying to get the “id” property you won’t get any since it’s an array, which is why your code doesn’t work. So in your initial code sample, I was suggesting to replace line 5 with $page->author->has($item), like so: // Grab other pages' content $authors = $pages->find("template=author"); <?php foreach ($authors as $item) : ?> <?php if ($page->author->has($item)) : ?> <?= $item->title ?> <?php endif ?> <?php endforeach ?> Thank you. I'm not completely understand what I did, but it seems to work now. <?php foreach ($page->author as $item) : ?> <?= $item->title ?> <?php endforeach ?> Link to comment Share on other sites More sharing options...
da² Posted September 20, 2023 Share Posted September 20, 2023 9 hours ago, Boost said: I'm not completely understand what I did, but it seems to work now. What do you need to understand? $page->author is a Page Reference field that can list multiple authors pages, so authors are stored in a PageArray, a kind of array dedicated in listing ProcessWire pages. So in your loop, $item is a Page instance using the template "author", and the Page class has a property "title" that corresponds to the title filled in this author's page admin. 2 Link to comment Share on other sites More sharing options...
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