Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active December 30, 2015 15:52
Show Gist options
  • Save timersys/04e016f703623eb7f72f to your computer and use it in GitHub Desktop.
Save timersys/04e016f703623eb7f72f to your computer and use it in GitHub Desktop.
Geotargeting with ACF 5
<?php
/*
* A very basic example file that will use Advanced custom fields - http://www.advancedcustomfields.com/
* along with Geotargeting Pro - https://timersys.com/geotargeting/
* to display a simple image or iframe base on user Geolocation
*/
?><div id="content">
<?php /* wordpress loop */ ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// repeater field
if( have_rows('urls') ):
// loop through the rows of data
while ( have_rows('urls') ) : the_row();
// geot field
// to debug you could print_r( $geot_games );
$geot_games = get_sub_field('geotargeting');
// text field
$game_url = get_sub_field('url');
// radio field
$url_type = get_sub_field('url_type');
// if geot_condition == include means "show to"
if( $geot_games['geot_condition'] == 'include' ) {
if( geot_target( '', $geot_games['geot_regions']) ){
// a simple function I use to print the iframe/ image not needed on this example
show_game_frame( $game_url, $url_type );
// if we have a match we simple get out of the repeater loop
break;
}
} else {
// we target the same countries but adding ! as we want to "exclude" matched regions
if( ! geot_target( '', $geot_games['geot_regions']) ) {
show_game_frame( $game_url, $url_type );
break;
}
}
endwhile;
endif;
?>
</div>
<?php endwhile; endif;?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment