
Hi everybody,
I would like to know if something like this is possible :
I created a custom field group with a custom field called “relation”, visible only if the page template is equal to “map”.
When I create a new page with template map, I can select several posts, from different custom post type, within the RELATIONSHIP field (called relation), all custom posts shares the custom field called “location”.
Thanks to documentation, I can easily list all posts in the page .
<?php
$posts = get_field('relation');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php echo the_field('spot_lieu'); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
But now comes the big challenge,
I want to display onto an .acf-map div all the markers from all posts
Someone have an idea ??
Cheers,
Damien
<?php
$posts = get_field('relation');
if( $posts ): ?>
<div class="acf-map">
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT)?>
<?php $location = get_field('spot_lieu', $p->ID); ?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4><?php echo get_the_title( $p->ID ); ?></h4>
<p class="address"><?php echo $location['address']; ?></p>
<p><?php echo get_the_post_thumbnail($p->ID); ?></p>
</div>
<?php endforeach; ?>
</div>
<ul>
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
<li>
<a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
<span><?php the_field('spot_lieu', $p->ID); ?></span>
<p><?php echo get_the_post_thumbnail( $p->ID); ?>
</p>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>