Home › Forums › Front-end Issues › Help Please: Page Links with Page Title
Hello,
I have a custom field named ‘related_products’ with these attributes:
Field Type: Page Link
Post Type: All
Select Multiple Values: Yes
I’m trying to display other pages in my WP site that relate to a particular product. ACF is working find, but I can’t seem to get the code right in the template to display all of the pages I have selected as being related to related to a product.
If I have only one related page, the code below works. I know it needs a loop around it but can’t quite get it right. I know the answer is easy, but I can’t get it working. Help, please.
I want the links to display the page’s title and the link to go to the page itself.
Here is what I current have. It works, but for only one link.
<?php
if(get_field('related_products'))
{
//echo '' . get_field('related_products') . ' : ' . get_the_title(get_field('related_products', 'id')) . '
';
echo '<a href="'.get_field('related_products').'">'.get_the_title(get_field('related_products', 'id')).'</a>';
}
?>
Thanks for your help!
Hi @azuremarcom
It is never smart to assume functionality. The code:
get_field('related_products', 'id')
will not work. Please always read the documentation which has been written for you to understand what parameters are available.
The page link field is very basic and will only return a url, nothing more. If you wish to get the title as well, you will need to change the field type to post_object. This will use the same data as page_link (so you will not lose any data) but your code will obviously have to change to allow for the new returned data.
Please read the docs to learn more about this field type
Thanks
E
Elliot,
Thank you for your reply although it only helped me know what I already knew; that what I was doing was not correct.
I changed the field type to Post Object and started working on it again. The code in the documentation did not work for me. It would only return links to the current page, not the related pages.
I ended up with the code below which worked.
<?php
$post_objects = get_field('related_products');
//var_dump($post_objects);
if( $post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post_object): ?>
<li>
<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
?>
dear @elliot
i tried to use the same code he did but i got nothing with my filed and i changed it to be post objects from page link this my code
<?php
$post_objects = get_field('related_products');
//var_dump($post_objects);
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
<?php endforeach; ?>
<?php endif;
?>
so what should i do
Regards
The topic ‘Help Please: Page Links with Page Title’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.