Last active
November 12, 2015 21:39
-
-
Save JiveDig/e0e862cb56e4814d9275 to your computer and use it in GitHub Desktop.
Create shortcode to display map of all child page locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Put this code in functions.php or a custom plugin. Don't include opening php tag | |
/** | |
* Create shortcode to display map of all child page locations | |
* | |
* @author Mike Hemberger | |
* @link http://thestizmedia.com/shortcode-to-show-map-markers-of-child-pages-with-acf-pro/ | |
* | |
* @return mixed | |
*/ | |
add_shortcode('child_pages_map', 'tsm_do_child_pages_map'); | |
function tsm_do_child_pages_map( $atts ){ | |
// Enqueue our scripts (previously registered) | |
wp_enqueue_script('google-map'); | |
wp_enqueue_script('amc-map'); | |
// Get our parent page ID - defaults to current page | |
extract( shortcode_atts( array('parent' => get_the_ID() ), $atts ) ); | |
// Show Posts | |
$args = array( | |
'post_type' => 'page', | |
'posts_per_page' => '-1', | |
'post_parent' => $parent, | |
); | |
$post_type = new WP_Query( $args ); | |
$output = ''; | |
// If our query found any posts | |
if ( $post_type->have_posts() ) { | |
$output .= '<div class="acf-map">'; | |
// Loop through the posts | |
while ( $post_type->have_posts() ) : $post_type->the_post(); | |
// Get each posts location | |
$location = get_field( 'data_facility_location' ); | |
// If the post has a location set, show the marker | |
if ( $location ) { | |
$output .= '<div class="marker" data-lat="' . $location['lat'] . '" data-lng="' . $location['lng'] . '">'; | |
$output .= '<a style="display:block;text-align:center;" href="' . get_the_permalink() . '">' . get_the_title() . '</a>'; | |
$output .= '</div>'; | |
} | |
endwhile; | |
$output .= '</div>'; | |
// Include CSS for our map (You can move this to your stylesheet) | |
$output .= '<style type="text/css"> | |
.acf-map { | |
width: 100%; | |
height: 300px; | |
margin-bottom: 20px; | |
} | |
.acf-map img { | |
max-width: none; | |
} | |
</style>'; | |
} // if | |
wp_reset_postdata(); | |
// Output our data | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment