In this post we’ll see how to geolocate a blog post, and how to show those located posts on a map. A very handy feature for travel blogs, hotel websites and so on.
Previously we saw how to create maps with markers in Leaflet Map. We even saw how to use those markers in a more advanced way and give them a bit of a boost. Now we’re going one step further with featured maps and overview maps. To do this, let’s take the following example. Imagine you have a travel website where each post is a destination.
It would make sense for each post about a destination to show a map with its location. This is what we call a featured map.
On the other hand, on the page where we show the list or loop with all the destinations, it would make sense to show a map where each one is located with a marker. And, of course, without having to do it by hand, but automatically.
Geolocating posts
To geolocate posts, we need custom fields. They can be the native WordPress ones, or you can use any other plugin such as ACF to create those fields easily.
In this example, to keep things simple, we’ll use the native WordPress custom fields. Remember that to enable them you need to click the three dots at the top right of the WordPress editor, then click Preferences, and turn on the Custom fields toggle, which you’ll find near the bottom.
Remember too that the custom fields are in a metabox at the bottom of the editor, labelled Meta boxes. Drag that panel up and you’ll see the custom fields.
Keep in mind that we’re in the post for a specific destination, and that destination must have latitude and longitude coordinates, which you can look up in Google Maps or OpenStreetMap. These are the coordinates we’re going to add as custom fields, so the fields we’ll define are:
geo_latitude: latitude coordinate, always using a dot for decimals. Example:18.90839958580139geo_longitude: longitude coordinate. Example:-70.08178710937501geo_address: the address we’ll use in the label, in this caseSanto Domingo, Rep. Dominicana.
Featured map with [featured-map]
Now that we have the coordinates linked to our post, it’s time to show the featured map. I, for example, want to show it at the start of the post, or right after its first paragraph.
In principle, just adding the [featured-map] shortcode, with nothing else, would be enough. Of course, you can customize the map with all the parameters this command accepts, but here I’ve kept it simple so it’s easier to understand. So in the editor we’d have the following:

And on the frontend we’d now see the map published with the location point we defined in the post’s custom fields.

But imagine you want to make the map a bit more elaborate. In the following example we can add a popup, give the marker a color, put a small symbol inside it, and so on.
[featured-map zoom=9 height=200 popup=geo_address marker=leaflet-extramarker markerColor=blue shape=circle icon=fa-house prefix=fa extraClasses=fa-5x iconColor=#98A700]This way we can see how the marker’s appearance on the map would change.

Another example would be the following, where we repeat the previous shortcode but the coordinates in the custom fields are different.

As we can see, the frontend reflects the corresponding result.

The next step is to build the automatic overview map.
Automatic overview map with [overviewmap]
Now what we want is to show, on an overview map, all the markers of the posts we’ve created, thanks to having positioned them with the coordinates of those places in the custom fields. We’ll build this overview map with the [overviewmap] command.
Optionally you can also add the overview-icon attribute, which sets the marker icon for that particular post, and we can show the featured image in the popup with the show_thumbnails=1 attribute.
To create our very, very basic map, we’d do the following:
[leaflet-map fitbounds]
[overviewmap latlngs=codex]
[hover]Notice that, so it knows where to take the coordinates from, we’ve told it they come from the WordPress geolocation system, that is, the standard WordPress fields we used (geo_latitude and geo_longitude).

And finally, it will show us the map we want, with the markers for the posts we’ve added coordinate fields to.

What’s more, if the posts are organized into categories, we can also filter those categories on the map as follows.
[leaflet-map fitbounds]
[overviewmap category="Asia"]Or we could filter by a list of categories:
[leaflet-map fitbounds]
[overviewmap category="Asia,África"]And if we add AND at the start, it will only show posts that belong to all the categories in the list at once, as if we were running an SQL query:
[leaflet-map fitbounds]
[overviewmap category="AND,aventura,historia"]If we now wanted to use a featured image and add a link to the category:
[leaflet-map fitbounds]
[overviewmap show_thumbnails thumbsize="100,100" show_category newtab]Imagine the map doesn’t show the markers you expect. In that case, the debug parameter will generate a table with all the posts the plugin is reading and their custom fields:
[leaflet-map fitbounds]
[overviewmap debug]This lets you see exactly what data the plugin finds in each post and where the error is. Something like what you can see below.

Linking to markers with [targetmarker] and [targetlink]
Now imagine you want to create a link anywhere on your site that, when clicked, takes the user to a map and automatically opens the popup of a specific marker.
On the same page
If the link and the map are on the same page, you only need to add [zoomhomemap] to the map and then create the link with [targetlink]. It’s important for the marker to have a title, since that’s the connection we’ll use. So we can create this map:
[leaflet-map fitbounds]
[leaflet-marker lat=37.1760 lng=-3.5880 title="Alhambra"]Alhambra[/leaflet-marker]
[leaflet-marker lat=37.1767 lng=-3.5994 title="Catedral de Granada"]Catedral de Granada[/leaflet-marker]
[leaflet-marker lat=37.1771 lng=-3.5902 title="Museo de la Alhambra"]Museo de la Alhambra[/leaflet-marker]
[zoomhomemap]And anywhere in the text on the same page:
[targetlink title="Alhambra" linktext="Ver la Alhambra en el mapa"]
[targetlink title="Catedral de Granada" linktext="Ver la Catedral en el mapa"]
[targetlink title="Museo de la Alhambra" linktext="Ver el Museo de la Alhambra en el mapa"]
[targetlink title="Jardines del Generalife" linktext="Ver los Jardines del Generalife en el mapa"]As you can see, it behaves exactly like an anchor within a web page, except that it takes us to a specific marker on the map and opens its popup.

On different pages
But if the link is on one page and the map on another, the destination map needs the [zoomhomemap] and [targetmarker] shortcodes:
[leaflet-map fitbounds]
[leaflet-marker lat=37.1760 lng=-3.5880 title="Alhambra"]Alhambra[/leaflet-marker]
[leaflet-marker lat=37.1767 lng=-3.5994 title="Catedral de Granada"]Catedral de Granada[/leaflet-marker]
[zoomhomemap]
[targetmarker]And on the source page:
[targetlink title="Alhambra" link="https://tudominio.com/mapa-granada/" linktext="Ver la Alhambra en el mapa"]
From an external site
Let’s take it a step further. What if we want to link to the map from another site? For that we’ll use a query string directly in the URL, with no shortcode:
https://tudominio.com/mapa-granada/?lat=37.1760&lng=-3.5880
The zoom parameter is optional: ?lat=37.1760&lng=-3.5880&zoom=16
It also works with GeoJSON
And if you use GeoJSON, can you use it too? Yes, [targetlink] also works with GeoJSON. Look:
[targetlink property=name value="Alhambra" link="https://tudominio.com/mapa-granada/" linktext="Ver en el mapa"]
⚠️ Note that the order of the shortcodes on the destination map is fixed:
[cluster]→[zoomhomemap]→[targetmarker]. If you have several maps on the same page,[targetmarker]can only appear once and must be the last shortcode.
Conclusions
As you can see, Extensions for Leaflet Map offers amazing possibilities when working with dynamic markers, to the point of making them behave like links to web pages.
If you want more information, I recommend exploring the examples in the Extensions for Leaflet Map documentation, which seem almost endless.

Leave a Reply