Home › Forums › Front-end Issues › Show field from relationship › Reply To: Show field from relationship
The result of the relationship field is going to be an array of pages, even if only one is selected. So you would either need to loop through them or target the array index. Furthermore the result of each item in the array is an object.
The loop would be:
$companies = get_field('company');
foreach($companies as $company){
$company_name = $company->post_title;
echo $company_name;
}
To target the first relationship in the array:
$companiy = get_field('company');
$company_name = $company[0]->post_title;
echo $company_name;
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.