joe_ma Posted February 9, 2016 Share Posted February 9, 2016 Hello I have a repeater ("banner") with two fields, one a text field ("keyword"), the other one is a page reference field ("color"). How can I get the title of the page this field is referencing? I have tried this $slogan = $page->get("banner"); foreach ($slogan as $s){ echo "<div class='" . $s->color . "'>"; echo "<p>" . $s->keyword . "</p></div>"; } This way I get the ID of the "color" page selected. As I need the title of this page, I tried echo "<div class='" . $s->color->title . "'>"; But then the output is empty. How do I get the title? 1 Link to comment Share on other sites More sharing options...
justb3a Posted February 9, 2016 Share Posted February 9, 2016 try echo "<div class='" . $pages->get($s->color)->title . "'>"; If $s->color contains the foreign page id, this should work. Link to comment Share on other sites More sharing options...
joe_ma Posted February 9, 2016 Author Share Posted February 9, 2016 Hmm … nope. This inserts the class "Home". Link to comment Share on other sites More sharing options...
kongondo Posted February 9, 2016 Share Posted February 9, 2016 (edited) Is the page field single or multi? If multi then you need to loop through the $s as well Edited February 9, 2016 by kongondo it was ok after all Link to comment Share on other sites More sharing options...
justb3a Posted February 9, 2016 Share Posted February 9, 2016 What outputs $s->color ? I thought it contains the id of the foreign page from which you want to get the title... If it is already a Page object (it should..) you don't have to get the page again. Link to comment Share on other sites More sharing options...
joe_ma Posted February 9, 2016 Author Share Posted February 9, 2016 What outputs $s->color ? I thought it contains the id of the foreign page from which you want to get the title... Yes, exactly. So, I really don't understand, why $s->color->title doesn't work. I found a solution, that does work now. $slogan = $page->get("banner"); foreach ($slogan as $s){ $farbe = $pages->get("id=$s->color"); echo "<div class='" . $farbe->title . "'>"; echo "<p>" . $s->keyword . "</p></div>"; } Link to comment Share on other sites More sharing options...
kongondo Posted February 9, 2016 Share Posted February 9, 2016 Hmm, is this relevant? https://processwire.com/talk/topic/8962-selectable-pages-defined-dynamically-for-page-field-in-repeater/ Link to comment Share on other sites More sharing options...
justb3a Posted February 9, 2016 Share Posted February 9, 2016 Yes, exactly. So, I really don't understand, why $s->color->title doesn't work. $s->color contains an id. An integer value. You cannot get a property of an integer. You have to get the corresponding object first. Or of what type is $s->color? If it is a string you have to get the integer value... Actually it's enough to pass the id to $pages->get(), it's not necessary to build a selector in this case. I thought chaining works as well... Link to comment Share on other sites More sharing options...
kongondo Posted February 9, 2016 Share Posted February 9, 2016 (edited) No need for that get again. $s is already an object...just foreach it and output its properties...This works (in my tests) $page->banners here is the repeater field. And you have to foreach, if the page field is multi foreach ($page->banners as $banner){ foreach ($banner->page_field_name as $b) {// page field in repeater is 'page_field_name' echo "<div class='" . $b->title . "'>"; echo "<p>" . $b->keyword . "</p></div>"; } } $s->color contains an id. An integer value. You cannot get a property of an integer. You have to get the corresponding object first. Or of what type is $s->color? If it is a string you have to get the integer value...Actually it's enough to pass the id to $pages->get(), it's not necessary to build a selector in this case. I thought chaining works as well... $s->color is actually not an id ...That's just the toString() method kicking in. It is an object Edited February 9, 2016 by kongondo more typos 1 Link to comment Share on other sites More sharing options...
joe_ma Posted February 9, 2016 Author Share Posted February 9, 2016 it's not necessary to build a selector in this case. Oh, but it seems so. $pages->get($s->color) doesn't work. Thanks anyway. @Kogondo: I'll have a look into pageTables; haven't used them so far. Link to comment Share on other sites More sharing options...
kongondo Posted February 9, 2016 Share Posted February 9, 2016 There was a typo in my code. I have corrected it. See my example again. Repeaters work just fine with page fields Link to comment Share on other sites More sharing options...
Jan Romero Posted February 9, 2016 Share Posted February 9, 2016 I'm confused. What @kongondo says is undoubtedly true: a page field will return a Page object, even from within a repeater. I just tested it myself. But knowing that, @joe_ma's code from the OP should have worked: echo "<div class='" . $s->color->title . "'>"; Here, $s is the repeater item, color is its page field, and title is the title property of that page field's page. Seems fine to me? Edit: Oh, I get it. The page field "color" is multi-page, so it returns a PageArray which silently fails to return a title property. @joe_ma, try changing color to a single page field, unless you need to define multiple colors per repeater item. Then your original code should work. It's a good idea to name your fields according to what they return, i.e. this one is called "color" as opposed to "colors", so when you access it, you're likely to expect a single item 1 Link to comment Share on other sites More sharing options...
justb3a Posted February 9, 2016 Share Posted February 9, 2016 Check your field settings, tab Detail. There you can define, what do you want to receive. If your field will contain multiple pages, then you should select the first option (PageArray). If your field only needs to contain a single page, then select one of the single Page options. Select one of the single Page options and your initial code should work. If you want to get multiple pages, you need to use the second foreach. 2 Link to comment Share on other sites More sharing options...
kongondo Posted February 9, 2016 Share Posted February 9, 2016 Check your field settings, tab Detail. There you can define, what do you want to receive. If your field will contain multiple pages, then you should select the first option (PageArray). If your field only needs to contain a single page, then select one of the single Page options. Select one of the single Page options and your initial code should work. If you want to get multiple pages, you need to use the second foreach. @justb3a is right. Double facepalm on my part! Even after asking whether your page field was single or multi, I forgot to check in my case that the setting @justb3a refers to was correctly set in my tests...OK, time for my evening tea Link to comment Share on other sites More sharing options...
joe_ma Posted February 9, 2016 Author Share Posted February 9, 2016 Yessss!! Thanks a lot to all of you. With the page field set to a single page, my OP code works fine. OK, time for my evening tea Exactly. 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