I'm building a website with a page that shows all locations (Custom Post Type) on a Google map. I use ACF (Advanced Custom Fields) to add the map location to the specific location. Furthermore I give each location a category (Parking, Hotel, ect.).
At this point all locations are show on the map correctly. But I'm trying to find a way to toggle these locations based on their category. So initially I want to hide all markers on the map and toggle their visibility based on a click (anchor or checkbox, doesn't matter).
I did see some answers that looked promising, but I can't figure this out with the code provided by ACF. I hope someone can help me with this. And can tell me where to add some extra code to setvisibility and toggle this based on a category select.
The code provide by ACF is shown below:
<script type="text/javascript">
(function($) {
/*
* new_map
*
* This function will render a Google Map onto the selected jQuery element
*
* #type function
* #date 8/11/2013
* #since 4.3.0
*
* #param $el (jQuery element)
* #return n/a
*/
function new_map( $el ) {
// var
var $markers = $el.find('.marker');
// vars
var args = {
zoom : 16,
center : new google.maps.LatLng(0, 0),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
// create map
var map = new google.maps.Map( $el[0], args);
// add a markers reference
map.markers = [];
// add markers
$markers.each(function(){
add_marker( $(this), map );
});
// center map
center_map( map );
// return
return map;
}
/*
* add_marker
*
* This function will add a marker to the selected Google Map
*
* #type function
* #date 8/11/2013
* #since 4.3.0
*
* #param $marker (jQuery element)
* #param map (Google Map object)
* #return n/a
*/
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map
});
// add to array
map.markers.push( marker );
// if marker contains HTML, add it to an infoWindow
if( $marker.html() )
{
// create info window
var infowindow = new google.maps.InfoWindow({
content : $marker.html()
});
// show info window when marker is clicked
google.maps.event.addListener(marker, 'click', function() {
infowindow.open( map, marker );
});
}
}
/*
* center_map
*
* This function will center the map, showing all markers attached to this map
*
* #type function
* #date 8/11/2013
* #since 4.3.0
*
* #param map (Google Map object)
* #return n/a
*/
function center_map( map ) {
// vars
var bounds = new google.maps.LatLngBounds();
// loop through all markers and create bounds
$.each( map.markers, function( i, marker ){
var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
bounds.extend( latlng );
});
// only 1 marker?
if( map.markers.length == 1 )
{
// set center of map
map.setCenter( bounds.getCenter() );
map.setZoom( 16 );
}
else
{
// fit to bounds
map.fitBounds( bounds );
}
}
/*
* document ready
*
* This function will render each map when the document is ready (page has loaded)
*
* #type function
* #date 8/11/2013
* #since 5.0.0
*
* #param n/a
* #return n/a
*/
// global var
var map = null;
$(document).ready(function(){
$('.acf-map').each(function(){
// create map
map = new_map( $(this) );
});
});
})(jQuery);
</script>
The code that renders my markers is as follows:
<div class="acf-map">
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php
$location = get_field('google_maps_adres');
if( !empty($location) ):
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4 class="marker__title"><?php the_title(); ?></h4>
<div class="marker__address">
<?php $contact_address = get_field('google_maps_adres');$address = explode( "," , $contact_address['address']); ?>
<div class="marker__address-street"><?php echo $address[0].', ';?></div>
<div class="marker__address-city"><?php echo $address[1];?></div>
</div>
<a class="marker__link" href="<?php the_permalink();?>">Meer informatie</a>
</div>
<?php endif; ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
</div>
I could help with this but maybe it's already answered here. Take a look and try the solution proposed there. If that doesn't work for you let me know and we can work it together ;)
EDIT:
Ok, let´s try to do this then. First of all you need to distinguish each marker, with a class or an ID. Let's do it with classes since we are using the categories and each marker can belong to several categories.
Change your code as follows:
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php $cats = get_the_category(); //this gives me the categories of the post (marker) ?>
<?php $catsString = implode(", ",$cats); // this gives me the categories as a String separated with ", " ?>
<?php
$location = get_field('google_maps_adres');
if( !empty($location) ):
?>
<div class="marker <?php echo $catsString; ?>" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<h4 class="marker__title"><?php the_title(); ?></h4>
<div class="marker__address">
<?php $contact_address = get_field('google_maps_adres');$address = explode( "," , $contact_address['address']); ?>
<div class="marker__address-street"><?php echo $address[0].', ';?></div>
<div class="marker__address-city"><?php echo $address[1];?></div>
</div>
<a class="marker__link" href="<?php the_permalink();?>">Meer informatie</a>
</div>
<?php endif; ?>
<?php endwhile;?>
All I've done is for each marker get the categories and used them as classes for the main marker container.
Now you have all your markers with their classes set. That's good. The next step is to use some input to control the visibility of those markers. Let's use a checkbox for example.
For each category you should have a checkbox:
<?php $categories = get_categories();
foreach( $categories as $category ) {
echo '<label><input class="markerToggle" type="checkbox" id="'. $category->name .'">'. $category->name .'</label>';
}
?>
Then you would need some simple Javascript to hide/show each marker
$('.markerToggle').change(function() {
if($(this).is(":checked")) {
//'checked' event code
$('.marker.' + this.id).show();
return;
}
$('.marker.' + this.id).hide();
//'unchecked' event code
});
Related
I am having products on backorder in a woocommerce store.
I am trying to create an error message if the value of the quantity input field is a higher/exceeds the products stock - see image below.
I also want this to go away if the customer goes below current stock.
If possible I also want the error to show in the cart page as well.
This is what I got this far:
function woocommerce_stock_now() {
global $woocommerce, $product;
?>
<script>
jQuery(function ($) {
var stocknow = <?php echo $qty = $product->get_stock_quantity()(); ?>;
var qtyinput = $('[name=quantity]').val();
var errormessagestock = '<p class="errormessagestock">'(stocknow.value - qtynow.value) . ' items are on backorder and will have a little longer delivery time.</p>';
$('#qtyinput').html(this.value);
$('[name=quantity]').change(function () {
if (qtyinput.value > $stocknow) {
$('stock').html(errormessagestock);
}
});
console.log("qtynow", this.value);
});
</script>
<?php
}
Tyr this:
add_action( 'woocommerce_single_product_summary', 'woocommerce_stock_now' );
function woocommerce_stock_now() {
global $product;
$stocknow = $product->get_stock_quantity();
?>
<script>
jQuery(document).on('input change','[name=quantity]',function() {
var stocknow = '<?php echo $stocknow; ?>';
var qtyinput = jQuery(this).val();
var overdue = parseInt(qtyinput) - parseInt(stocknow);
if (parseInt(qtyinput) > parseInt(stocknow)) {
var errormessagestock = '<p class="errormessagestock">('+overdue+') items are on backorder and will have a little longer delivery time.</p>';
console.log(errormessagestock);
//$('stock').html(errormessagestock);
}
});
</script>
<?php
}
console.log(errormessagestock) will return your message now you can set/print this message accordingly.
I want show price * quantity in woocommerce single product page. Codes below is working but until quantity does not change shows NAN for price.
add_action( 'woocommerce_before_add_to_cart_button', 'woocommerce_total_product_variation_price' );
function woocommerce_total_product_variation_price() {
global $woocommerce, $product;
// setup base html... We are going to rewrite this with the standard selected variation
echo sprintf('<div id="product_total_price" style="margin-bottom:20px; display: block;">%s %s</div>',__(' price:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var currency = currency = ' <?php echo get_woocommerce_currency_symbol(); ?>';
function priceformat() {
var product_total = parseFloat(jQuery('.woocommerce-variation-price .amount').text().replace(/ /g ,'').replace(/€/g ,'').replace(/,/g ,'.')) * parseFloat(jQuery('.qty').val());
var product_total2 = product_total.toFixed(3);
var product_total3 = product_total2.toString().replace(/\./g, ',');
jQuery('#product_total_price .price').html( product_total3 + ' ' + currency );
}
jQuery('[name=quantity]').change(function(){
priceformat();
});
jQuery('body').on('change','.variations select',function(){
priceformat();
});
priceformat();
});
</script>
<?php }
I am using Advanced Custom Fields to set the location of a post and display it on a map within my custom post template and it works great. But I have also created a google map that plots all the locations of my custom posts types on one map.
My problem is that I can’t get it to pull content or a link into the infowindow from those custom posts. I also can't get it to change the marker depending on a true/false field.
I’ve looked around the internet and tried to use the code that google suggests as well as the users on this site, but I seem to be doing something wrong. All it does is display the title followed by a weird symbol. Any help would be appreciated :)
This is the code I am using:
<section>
<?php
$args = array(
'post_type' => 'offices',
'posts_per_page' => -1
);
// query
$wp_query = new WP_Query( $args );
$NUM = 0;
?>
<div id="map"></div>
<script src='http://maps.googleapis.com/maps/api/js?sensor=false' type='text/javascript'></script>
<script type="text/javascript">
var locations = [<?php while( $wp_query->have_posts() ){
$wp_query->the_post();
$location = get_field('location'); // IMPORTANT << Change this for your Google map field name !!!!!!
?>
['<?php the_title(); ?>', <?php echo $location['lat']; ?>, <?php echo $location['lng'];?>, <?php $NUM++ ?> ],
<?php } ?> ];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 7, /*Here you change zoom for your map*/
center: new google.maps.LatLng(42.6, -76.0), /*Here you change center map first location*/
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</section>
Yes, I got your problem. As per me you are doing well but you need to modify your code slightly.
Just make an array of values in your while loop like for markers :
$marker[] = '"'.get_the_title().'"'.','.$event_lat.','.$event_long;
and for marker details like :
$marker_details[] = get_the_title().'_sunil_'.get_the_content($post->ID).'_sunil_'.$ev_location.'_sunil_'.get_permalink($post->ID);
Then after this just place your script using foreach loop and explode above $marker_details array by separator sunil.
The code will be :
var markers = [
<?php foreach ($marker as $mar){
echo "[".$mar."],";
};?>
];
var infoWindowContent = [
<?php foreach ($marker_details as $mar_det){
$check_val = explode('_sunil_',$mar_det);?>
['<div class="info_content">' +
'<h3 style="display:block"><?php echo $check_val[0];?></h3><strong style="display:block;text-transform:capitalize;"><?php echo "Location : ".$check_val[2];?></strong>' +
'<p><?php echo substr($check_val[1], 0, 200);?>...</p>Read More' + '</div>'],
<?php }?>
];
Do not disturb structure by even a single comma while making it dynamic using loops. Just take a fresh html and implementation instructions from here : http://www.wptricks24.com/map-with-multiple-markers
For the life of me, I can not get this to work. I'm trying to pass the Marker Clusterers to my Google Map - but it's not working. I've got this map set up through Google Maps API and it's for a Wordpress website. The user can add locations using a Custom Post type. There are a lot of locations in close proximities, so I'm trying to use the MarkerCluster to lighten it up a bit. I've never used the MarkerCluster before, so it could be a small thing, too... not sure.
'var mc = new MarkerClusterer(map);' --This draws an error of: "Uncaught ReferenceError: MarkerClusterer is not defined"
Any insight? Any help is greatly appreciated!!
<?php // Index template
get_header(); ?>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<div class="twelve column">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="intro">
<?php the_excerpt(); ?>
<hr>
</div>
<?php the_content(); ?>
<header class="clearfix"></header>
<div id="mapcanvas"></div>
<?php
// For creating multiple, customized loops.
// http://codex.wordpress.org/Class_Reference/WP_Query
$custom_query = new WP_Query('post_type=locations'); // exclude category 9
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<?php if(get_field('link')): ?>
<div>
<?php while(has_sub_field('link')): ?>
<div>
<p><?php the_sub_field('url'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php
function get_single_term($post_id, $taxonomy)
{
$terms = wp_get_object_terms($post_id, $taxonomy);
if(!is_wp_error($terms)) {
return ''.$terms[0]->name.'';
}
}
$i = 0;
// For creating multiple, customized loops.
// http://codex.wordpress.org/Class_Reference/WP_Query
$custom_query = new WP_Query('post_type=location&posts_per_page=-1');
while($custom_query->have_posts()) : $custom_query->the_post();
$title = get_the_title(); // Location title
$map = get_field('location'); // ACF location contains address and coordinates
$terms = strip_tags( get_the_term_list( $post->ID, 'distributors', '', ' & ' )); // Get distributor terms and rm links
$info = '<strong>' . $title . '</strong><br>' . $map['address']; // Info window content
$link = get_field('link');
if($link){
$info .= '<br>'. $link .'';
}
$location[$i][0] = $title; // Store the post title
$location[$i][1] = $map['coordinates']; // Store the ACF coordinates
$location[$i][2] = json_encode($info); // Store info window content
$location[$i][3] = strip_tags( get_single_term( $post->ID, 'distributors' )); // Get first term for marker icon
$i ++;
endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<script>
$(function initialize() {
geocoder = new google.maps.Geocoder();
// Center map on our main location
var myLatLng = new google.maps.LatLng(41.583013,-93.63701500000002);
var bounds = new google.maps.LatLngBounds();
// https://developers.google.com/maps/documentation/javascript/styling
// Create an array of styles.
var styles = [
{
stylers: [
{ saturation: -99.9 }
]
}
];
// Create a new StyledMapType object, passing it the array of styles,
// as well as the name to be displayed on the map type control.
var styledMap = new google.maps.StyledMapType(styles, {name: 'exile'});
// Create a map object, and include the MapTypeId to add
// to the map type control.
var mapOptions = {
mapTypeId: 'roadmap',
center: myLatLng,
zoom: 14,
disableDefaultUI: false,
scrollwheel: true,
draggable: true
};
// Display a map on the page
var map = new google.maps.Map(document.getElementById("mapcanvas"), mapOptions);
var mc = new MarkerClusterer(map);
map.setTilt(45);
//Associate the styled map with the MapTypeId and set it to display.
map.mapTypes.set('exile', styledMap);
map.setMapTypeId('exile');
// Marker icons
typeObject = {
"Others" : {
"icon" : new google.maps.MarkerImage('http://exilebrewing.com/site/img/beer-mug.png', new google.maps.Size(18,26), new google.maps.Point(0,0), new google.maps.Point(9,26)),
"shadow" : new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png', new google.maps.Size(40,34))
}
}
// http://wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/
// Multiple Markers
var markers = [
["Exile Brewing Co", 41.583013,-93.63701500000002,"Others"],
<?php
if (count($location)>0) {
foreach ($location as $key => $value){
if ($key < (count($location)-1)){
echo '["' . $location[$key][0] . '",' . $location[$key][1] . ',"' . $location[$key][3] . '"], ' . "\n";
} else {
echo '["' . $location[$key][0] . '",' . $location[$key][1] . ',"' . $location[$key][3] . '"]';
}
}
}
?>
];
var markers = [];
for (var i = 0; i < 100; i++) {
var latLng = new google.maps.LatLng(data.photos[i].latitude,
data.photos[i].longitude);
var marker = new google.maps.Marker({'position': latLng});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
// Info Window Content
var infoWindowContent = [
["<strong>Exile Brewing Co.</strong><br>1514 Walnut Street, Des Moines"],
<?php
if (count($location)>0) {
foreach ($location as $key => $value){
if ($key < (count($location)-1)) {
echo '[' . $location[$key][2] . '], ' . "\n";
} else {
echo '[' . $location[$key][2] . ']';
}
}
}
?>
];
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
// Loop through our array of markers & place each one on the map
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]); // ACF coordinates
var icon = typeObject[markers[i][3]]['icon'];
var shadow = typeObject[markers[i][3]]['shadow'];
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0],
icon: icon,
shadow: shadow
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));
// Automatically center the map fitting all markers on the screen
//map.fitBounds(bounds);
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(15);
google.maps.event.removeListener(boundsListener);
});
});
</script>
<div id="map-canvas"></div>
<footer class="clearfix"></footer>
<?php endwhile;?>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
As per my comments :
You need to download the script ( here ) and then Include it in your page.
Marker clusterer is not a built-in feature of google maps , but an addon script ..
<script src="markerclusterer.js" type="text/javascript"></script>
or in the "correct" wordpress way :
wp_register_script( 'js_marker_clusterer', plugins_url( '/scripts/MarkerClusterer.js', __FILE__ ),'jquery' ); // or other dependencies descendents - preferably map JS or your custom.
wp_enqueue_script('js_marker_clusterer');
( change to your paths )
Also, look at the examples ( HERE ) and check their code ( and includes )
You need to include the third party marker clusterer script if you want to use it.
As specified in the documentation:
Note: Be sure to include markerclusterer.js or markerclusterer_packed.js in your HTML document.
<script src="/path/to/markerclusterer.js" type="text/javascript"></script>
I have flexslider installed on my wordpress and working great, the way it works, it grabs all the images attached to the page and displays them, is it possible to make every image links to its file?
the code I am using is this
<ul class="slides">
<php foreach ( $attachments as $attachment ) { ?>
<li><?php echo wp_get_attachment_image( $attachment->ID, 'cw-post' ); ?>
</li>
<?php } ?>
</ul>
I think this will do the trick.
// Get ID of the attached file
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
// Get attached file. Define what size at the end. DEfault is the thumbnail.
$image_src_arr = wp_get_attachment_image_src($thumbnail_id,'medium');
// Return image source
$image_source = $image_src_arr[0];
I've created this function for myeslf:
/**
* This function will return source url for attached image-
* The get_the_ID is a global WP command that will retrieve current post / page ID
* Using the post ID it will return the attachment ID needed in order to call
* wp_get_Attachment_image_src.
*/
function get_related_image($size){
$image_src_arr = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()),$size);
if(sizeof($image_src_arr) > 0) {
return $image_src_arr[0];
} else {
return false;
}
}