How do you when using custom fields in Wordpress echo just the first value using foreach?
Currently the code is:
<?php for(get_field('venue_event') as $post_object): ?>
<a href="<?php echo get_permalink($post_object); ?>"><?php echo get_the_title($post_object) ?></a>
<?php endforeach; ?>
This takes the field from the wordpress page (the field is a link to another page), creates a link to that page using get_permalink but when I want to echo the page title it does it, but then it also echos all other values that are not needed.
If you just want the execute the first iteration of the loop, try this:
<?php foreach(get_field('venue_event') as $post_object): ?>
<a href="<?php echo get_permalink($post_object); ?>"><?php echo get_the_title($post_object) ?></a>
<?php break; ?>
<?php endforeach; ?>
you can just add
$counter = 0;
<?php for(get_field('venue_event') as $post_object): ?>
$counter++;
if($counter == 1)
{
<a href="<?php echo get_permalink($post_object); ?>"><?php echo get_the_title($post_object) ?></a>
}
<?php endforeach; ?>
Wouldn't it then be easier just to use the first element of the returned array? Maybe Wordpress offers other filters that return the the page's title only.