Jump to content

How to get the title from a page field in a repeater


joe_ma
 Share

Recommended Posts

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?

  • Like 1
Link to comment
Share on other sites

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

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

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 by kongondo
more typos
  • Like 1
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...