Jump to content

Pagefield type - Get title


ankh2054
 Share

Recommended Posts

Hi all,

Struggling to get the title of a page field type. 

  • Have created a page called Almonds.
  • The almonds page has a page field type called food_types, where one page is selected.

If I load the page almonds using the below code I get the ID associated with that page field type, but I'm unsure of how to get the title.

<?=$page->food_types ?>

I have tried the below and get nothing, guessing I'm not doing it right. Could someone advise.

<h2><?=$page->food_types->name ?></h2>
<h2><?=$page->food_types->label ?></h2>
<h2><?=$page->food_types->title ?></h2>
 

thanks so much

Charles

Link to comment
Share on other sites

Is this a multiple page field? In that case you would have to iterate it with a foreach or get the first one with first()

<?php
foreach($page->food_types as $ft) {
    echo $ft->title;
}
?>

or

<?=$page->food_types->first()->title ?>
  • Like 3
Link to comment
Share on other sites

thanks very much, the first() method works successfully.
 
This is more of a PHP question than processwire, so accept my newbiness. I am using search.php to show a list of ingredients and their associated food_types.
 
<h5 class='list-group-item'>Type:{$m->food_types->first()->title}</h5>
 
Need to somehow add your foreach loop into an existing for each loop to ensure it shows all the selected pages and not only the first one.
 
Any ideas?
 
foreach($matches as $m) {

//Theme each search result

$out .= "<div class='media col-md-3'>
<a href='{$m->url}' class='list-group-item'>
                   <h5 class='list-group-item-heading'>{$m->title}</h4>
                   <h5 class='list-group-item'>Type:{$m->food_types->first()->title}</h5>
                   <p class='list-group-item-text'>{$m->body}</p>
        </a>
       </div>";
}

$out .= "</div>";

thanks for your help

Link to comment
Share on other sites

Did you try what Diogo mentioned about the looping of the pages?

foreach($matches as $m) {
	$out .= "
	<div class='media col-md-3'>
	  <a href='{$m->url}' class='list-group-item'>
	    <h5 class='list-group-item-heading'>{$m->title}</h4>";
	    foreach($m->food_types as $ft){ // Iterate through the pages here
		$out .= "<h5 class='list-group-item'>Type:{$ft->title} </h5>"; // Output the title here
	    }
	$out .= "
		<p class='list-group-item-text'>{$m->body}</p>
	  </a>
	</div>";
}
Link to comment
Share on other sites

Just a pointer for those interested in why the first() method that diogo mentioned solved this; if you look at your page field "food_types" and switch to the details tab, you probably have the first option selected, like this...
 
post-465-0-67188900-1413883087_thumb.png
 
...which is setup to return an array of pages, even if that array only has a single page in it. Now that's why you need to call first() as you need to select a page from the array that $page->food_types was returning.
 

/**
 * Examples for a page field setup to return an array of pages...
 */

echo $page->food_types; // This won't work for an array return as you haven't drilled down to a page entry yet.

$p = $page->food_types->first(); // Ah, now $p holds a page.
echo $p->title; // And now we can show the title.

echo $page->food_types->first()->title; // This works too, no need to assign to a variable if we don't want to.

 
You stated in your opening post that food_types only has one page selected...
 

<snip>

  • Have created a page called Almonds.
  • The almonds page has a page field type called food_types, where one page is selected.
<snip>

Now look at the other two options for your page field in the screen shot. Both of those options return a single value when the page field is referenced. If you had picked either of those two options then your code would have worked as you'd have been given a page (or 'false' or a NullPage) directly without having to fish it out of an array.
 
Anyway, hope that helps explain things a little more.

  • Like 7
Link to comment
Share on other sites

thanks all for your help, the below worked as per instructions.

foreach($matches as $m) {
	$out .= "
	<div class='media col-md-3'>
	  <a href='{$m->url}' class='list-group-item'>
	    <h5 class='list-group-item-heading'>{$m->title}</h4>";
	    foreach($m->food_types as $ft){ // Iterate through the pages here
		$out .= "<h5 class='list-group-item'>Type:{$ft->title} </h5>"; // Output the title here
	    }
	$out .= "
		<p class='list-group-item-text'>{$m->body}</p>
	  </a>
	</div>";
} 
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...