Hi,
In my posts I use various custom fields and everything works fine, except when I call the following field (see code below). Below this snippet on the front-end, no more content appears even though there should be more. When I delete this part, the content that should appear below works fine. I am not sure what is causing this but clearly it is something about this code or field?
The field ‘more’ is a Post Object where I select one or more pages that should be displayed as links in the front-end.
Any help would be appreciated, thanks!
$values = get_field('more');
if (!empty($values)) {
echo '<div class="container-btl"><div class="row">
<div class="col-md-4"></div>
<div class="col-md-8"><h2>'.$heading.'</h2>
<ul style="list-style:none;">';
foreach( $values as $post) {
setup_postdata($post);
$fname = get_post_meta($post->ID, 'first_name', true);
$lname = get_post_meta($post->ID, 'last_name', true);
$link = get_permalink($post);
}
echo '<li><a href="'.$link.'" >'.$fname.' '.$lname.'</a></li>';
echo '</ul></div></div></div>';
} else {
}
I checked the documentation again and using this code, it works:
$featured_posts = get_field('more');
if( $featured_posts ): ?>
<div class="container-btl"><div class="row">
<div class="col-md-4"></div>
<div class="col-md-8"><h2><?php echo $heading; ?></h2>
<ul style="list-style:none;">
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
?>
<li>
<a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
</li>
<?php endforeach; ?>
</ul></div></div></div>
<?php endif; ?>