You can display a map in your templates with the following code:

<?php if ( isset($wpgeo) ) $wpgeo->categoryMap(); ?>

This will display a map similar to the widget map with a marker for each post that is currently displayed. For example, you could insert this in your index, category and archive templates to show a map above the list of posts.

This ONLY shows markers for posts that are visible on the current page. It does not show markers for ALL posts in a category.

List of Template Tags

wpgeo_map() and get_wpgeo_map()

Outputs a map based on a WordPress query.

wpgeo_post_map() and get_wpgeo_post_map()

Outputs the post map – use within the post loop.

<?php if ( function_exists( 'wpgeo_post_map' ) ) wpgeo_post_map(); ?>

74 Responses to “Template Tags”

  1. paul says:

    hi!
    is there a shortcode which i can use in post’s body or a template tag to show the latitude/longitude values as text if there is a location for a post? i want to show the post’s location in the header of the post as text. how can i do this?

    thanks, paul

  2. Shane says:

    Is there a way to insert a tag into a page that will show a map mashup of all posts containing lat/long data?

    For example, you click on the Mashup page and it would display a map that would scale just large enough to display all the pushpins associated with posts that contain lat/long.

    I understand that for a large blog with many posts, this may be too much, but for smaller blogs it would kind of cool.

    • Ben says:

      Hi Shane, that is the most requested feature and I am working on it! I have to rebuild some of the plugin to get it to work but it WILL be added – sometime soonish hopefully…

    • Hi,
      really nice plugin indeed !!

      I wanted as well a mashup page with all my posts on the map, to do a “mapped archive” page, so I watched your code a bit and found a solution without modifying it :

      All you have to do is :
      * Create a new template page with the appropriate “Template Name : XXXX” declaration at the beginning

      * At the very beginning of the template, call you post query according to what you want to show on the map and assign it to the global $posts variable. For me :

      global $posts;
      $posts = get_posts(‘numberposts=-1&’);

      * Now call the get_header() template tag. This will trigger your hook in wp_geo.php that prepares map’s data.

      * Call the $wpgeo->categoryMap(); template tag in your template code to show the mashup map

      Now you can create a new page within WordPress and set the page template to this new one, and it should show up with the map !

      Thank you so much for this plugin, I’m gonna use it on my travel blog and it’s just perfect ๐Ÿ™‚
      Olivier

    • Ben says:

      Olivier, I like your solution… I might write that up into a post for people wanting to achieve the same sort of effect. Is a good solution until I get the RSS mashup version working properly.

    • Moises says:

      Olivier,

      Your solution works partially. The thing is that after calling the function get_posts you are changing the query and because of that WordPress ignores other things such as the page title and so on.

      Does anyone have another solution to display a map with all the posts from a category or similar?

  3. David Driscoll says:

    Great plug-in, wondering how I put a label on the tag attached to the marker, mine just appears blank??

    Thanks

  4. Tim says:

    Nice plugin!

    I would like to show a tab ONLY if wpGeo is installed and active, but I’m not sure exactly how to do that.

    Example:

    if(function_exists(‘wpgeo’)) then show tab…otherwise show nothing

    Which function should I check for?

    Thanks!!

  5. Tim says:

    Worked perfectly. Thanks!!

  6. Joe says:

    Thanks for this. My dad’s just started a travel site now he has retired and this worked great! Nice work.

    I note there are different sized markers, but I don’t see an option to change them. I’d like to use the larger ones?

    Also, be a nice feature to be able to specify a separate mini title, description and thumbnail image from the post page to spice it up a bit. That would be great. Any chance of any in that functionality?

    Great work pal.

  7. Jens says:

    Hello Ben,

    I’ve one question. This

    global $posts;
    $posts = get_posts(‘numberposts=-1&’);

    is for posting all flags in a map and it works!

    Now is this also possibile for posting flags in a map for only one category???

    Thanks Jens

  8. Jens says:

    Hi Ben,

    I tryed it with this 3 versions:

    But it doesnยดt work…. look here http://www.milchladen.de/weltkarte/map-cape-2-nairobi/

    I canยดt good php! Yes I know….shame over me ๐Ÿ™‚

  9. Jens says:

    Look this

    global $posts;
    $myposts = get_posts(‘numberposts=-1&offset=1&category=25’);

    global $posts;
    $myposts = get_posts(‘offset=1&category=25’);

    global $posts;
    $myposts = get_posts(‘category=25’);

  10. Jens says:

    Hi Ben,

    you’ve an solution for this problem??

    Thanks, Jens

  11. Anox says:

    Hi there!

    Really great plugin!

    I try to use the map in my search results template to display all locations according to the search term. However it does not work (no map is displayed).

    Any hints?

  12. Henrik says:

    Hi

    Is there a template tag to show the map on a single post/page. I want to do a check to see if I’m on a custom post and then only show the map on my custom post. Not in regular post/pages.

    Something like (havent tried this)
    Map(); ?>

  13. Is there a template tag to just display the name of the location? I am trying to use the pluging to automatically display the meta like this posted by in .

    Thanks,
    Steve Shoppman

  14. sekatsim says:

    Is there a way to set a custom map center and zoom with the template tag?

  15. Dave Jackson says:

    Ben, the earlier versions were working fine, but now the maps don’t show on our Beaches website which is the most prominent beach site on the internet. I’m not sure if it has to do with the Thesis theme we use. Any suggestions?

    Thanks! Dave

  16. Daniel says:

    Hello,
    I am trying to figure out how to add wp-geo to a custom post type in WP 3.0.1. I think I need to hook it into my my register_post_type() function but am unsure of the syntax needed. Can I simply include the wp-geo php file?

    • Ben says:

      How have you added the custom post type?
      In theory if you install WP Geo and go to the WP Geo settings page there should be a checkbox to turn on for your custom post type.

      Or when you’re registering your post type, when you set the things your post type supports (comments, revisions etc) you can add “wpgeo” to this list.

  17. Daniel says:

    Hi Ben,

    Thanx for the tip, I ended up using add_post_type_support but will change to using wpgeo in the post type registration function.

    Thanx again!

  18. Daniel says:

    sorry forgot to ask:

    For some reason my lats and longs show up using the template tags but not the map itself. The size is displayed but the map is all black. Any ideas?

    Thanx, Daniel

  19. Aaron says:

    Hi Ben,

    Nice plugin! Im having some trouble with templates though. maps work in posts if I use the shortcode but not when I reference them using your template code, im using the following.

    if ( isset($wpgeo) ) $wpgeo->categoryMap(); ?>

    Am I doing something wrong?

    Cheers

    Aaron
    .

  20. Aaron says:

    Sorry forgot to add Im using custom posts, again they work fine with shortcode but Im having a nightmare of a time trying to get it to work by referencing from a template.

    if ( isset($wpgeo) ) $wpgeo->categoryMap(); ?>

    Will the above work outside the loop?

  21. Aaron says:

    Sorry to hassle mate, Im going insane over here though. The plugin is working with my custom post but I cant make a map appear when referencing from a template.

    Ive tested other template tags like

    wpgeo_longitude() and wpgeo_latitude()

    and they display the correct strings so Im not sure whats up, hopefully you have some insight for me.

  22. Aaron says:

    Hi Ben,

    thanks for getting back to me, unfortunately its still not working for me.

    wpgeo_post_map() didnt display the map but it didnt give me any errors either.

    Ive tested a bit more and Im noticing the checkbox where I select my custom post is sometimes greyed out and when I change different settings it randomly unsets itself causing the maps not to show in my edit custom post pages.

    Ive tried installing the plugin again but no good.

    have you had trouble with this before?

    • Ben says:

      Aaron, do you mean the checkbox disabled when you change some of the WP Geo settings, or when you change the way your custom post type is set up?

    • Aaron says:

      Hi Ben,

      The checkbox disables (well it still has a tick in the box it just turns grey and can not be clicked).

      Yeah this happens when changing WP GEO settings.

  23. Jonathan says:

    I’m trying to build this into a template. Right now on the category page, and any post within my category, I can get a map to show with all the markers for that category. I’m using:

    wpgeo_map(‘cat=5’);

    I’m wondering if there is a way to customize the width, height, zoom, map type (hybrid), and controls that are visible.

  24. Ben says:

    I’ve just posted an update so you can pass width and height parameters, can’t currently customise zoom etc.

  25. Julie says:

    Hello !

    Thanks for your plugin, it is really great !! Just what I needed

    For design reasons, I wonder what is the code pour a map in a post (not the shortcode), it might be similar to : categoryMap ( ) ; ?>

    I need to put the map outside of the post (on the same page)

    Maybe postMap ( ) ; ?> or something like that ?

  26. Julie says:

    It doesn’t work unfortunately…

    It’s for this kind of page : http://www.carnets-de-traverse.com/mag/portfolio/delices-de-hoi-an/
    The zoom and the center of the map are not good. That might be because I use the code : categoryMap(); ?>

    thank you for your help ๐Ÿ™‚

  27. stefano says:

    hi evryone! compliment for this.

    Can someone make an exemple of full script to put in a template page for have:
    latitude
    longitude
    dimensions
    category exclude
    and type of map

  28. Anthony says:

    Hello,

    I too am having a problem with custom post types.

    It appears as though the check boxes cycle through their states every time you update the settings. There are no direct warnings however the wpgeo_map() leaves a white space (same as the map dimensions) where the map should be. It doesn’t affect my single post maps, only maps based on $wp_query iterating using custom post types.

  29. Jonathan says:

    Also looking for a way to set the center and zoom with wpgeo_map()

  30. Adam G says:

    Hi Ben,

    Firstly what a great plugin. Thanks for the development. On my blog i have posts from before 1970 (back dated for history purposes). I had to change the date function slightly in the main wp-functions file (stated here http://wordpress.org/support/topic/pre-1970-dates-no-longer-handled-correctly) for WP to pick up dates. This works.

    However WP-GEO seems to exclude posts since before 1970. Could you advise where and what to change for WPGEO? Many Thanks.

  31. Jason Day says:

    Hi,

    great plugin. I have it working with my custom post type, but I have a custom page template to show the loop of custom post types and I can’t get the map to show. I’m trying to get a map showing all posts outside of the loop and the code above doesn’t work for me.

    Thoughts?

    thanks!

    • Ben says:

      When you view the page, can you confirm if it is loading the Google Maps javascript file? If not try enabling WP Geo to work on pages in the WP Geo settings.

  32. Jonathan says:

    I’m working with a map that lists places around the world (with a heavy concentration North and Central America. The problem is that it chooses to center on the middle of the pacific ocean, with Europe on the far left of the map, and the Americas on the far right.

    In the past I’ve asked for a way to create a custom center for the map, but editing the plugin file with custom latitude and longitude would create a problem for me in other maps that are more local (for example, only within a certain region of South America).

    There must be something wrong with how the ‘bounds’ variable is calculated, because I can’t imagine anyone ever wanting their map to render like that.

  33. Jonathan says:

    Also, I’m having trouble with the map not showing up. It works fine on category archive pages and posts, but won’t load anything for a page. Is there something that I need to do to query posts?

  34. trusktr says:

    Hello Ben,

    It seems some people haven’t returned to this page to see your follow-up comments… perhaps you can use a plugin like “subscribe to comments”.. It works well for me!

    Nice plugin!!!!

  35. trusktr says:

    [ed: @trusktr, I’ve combined your multitude of comments into a single entry]

    Hey, I’m trying to use wpgeo_map() with a wordpress query to show all posts on a map. However, it is only showing the first four posts. Am I doing it correctly?

    Here’s what I’m doing: http://pastie.org/2080215

    I might also add that not all posts in my blog have a location, so perhaps wpgeo_map() is choking when it encounters the first post without a location?

    I can confirm that removing the other none-mapped posts is now allowing the map to show 5 mapped posts instead of 4… But now I’m stuck at trying to show more than 5. The query itself works fine because if I make a loop, it will show the content for all posts. wpgeo_map() just isn’t obeying the query for some reason… I’ve tested other map plugins so perhaps there might be post data that is interfering? Could that be possible?

    No matter what I try, I get the same result: no more than 5 markers on the map.

    wpgeo_map() by itself with no query: 5 markers
    Passing the WP_Query object into wpgeo_map(): 5 markers

    I just can’t figure it out hehe.

    Also, the template tag at the top of the page shows nothing for me (categoryMap(); ?>)

  36. trusktr says:

    mmm hmmmmm! I figured it out… I had to pass all the map attributes into wpgeo_map(). I figured it out by looking at shortcode_wpgeo_mashup()… so I modified shortcode_wpgeo_mashup() into my own function to use as a template tag. I named my function “my_wpgeo_mashup()” ๐Ÿ˜€

    If anyone’s interested in seeing how it is done, this is how:
    http://pastie.org/2080445

    I think the simple fix would be to modify wpgeo_map() so that it obeys default options when nothing is passed into it. ๐Ÿ˜‰

    I found where the fault is with wpgeo_map() (kinda)… If you don’t supply it with the correct attributes in the $query argument, then it won’t work like intended… it won’t even follow blog defaults… You should add the feature (where it will follow default options when $query variable is not passed in) or fix it. ๐Ÿ˜€

  37. trusktr says:

    aha, i just realized that my custom template tag doesn’t work without the widget enabled in the sidebar…

    That also means that wpgeo_map() doesn’t work unless a wpgeo widget is active because without the widget, the plugin doesn’t insert the needed javascript, so the template tag is not even useable at all without a widget activated.

  38. ab says:

    What is up with the categorymap template tag?

    Nothing seems to work with displaying the map with locations for all posts in teh category… Has anyone solved this?

    Not even passing the category id works fore example, wpgeo_map(‘cat=#’)

    • Ben says:

      This works OK for me:
      wpgeo_map('cat=4');

      By default it will show the same amount of posts as you have set in your WordPress settings. To show ALL posts in a category you need to also pass the ‘numberposts’ parameter:

      wpgeo_map('cat=4,numberposts=-1');

      or

      wpgeo_map(array('cat'=>4,'numberposts'=>-1));

  39. I need to order alphabetically the map markers of categoryMap ( ) ;

    How can I fix it?

    Thank you

  40. Finally I added this code before get_header() and works! Thank you.

    global $posts;
    $paged = (get_query_var(‘paged’));
    $posts = query_posts(“orderby=title&order=ASC&paged=$paged”);

  41. Clay says:

    I’m using wpgeo_post_map on a post single page where I would like to have a deeper zoom. Is this currently possible?

  42. Adrian says:

    Hi,
    I’ve upgraded to 3.3 version but it stopped working on my search page. I am using this tag:
    categoryMap(); ?>
    A also tried this:

    and this:
    With the last 2 tags the map shows but with no markers.
    What am I doing wrong?

  43. Adrian says:

    Hi,
    I’ve upgraded to 3.3 version but it stopped working on my search page. I am using this tag:
    if ( isset($wpgeo) ) $wpgeo->categoryMap();
    A also tried this:
    if ( function_exists( ‘wpgeo_post_map’ ) ) wpgeo_post_map();
    and this: echo get_wpgeo_map();
    With the last 2 tags the map shows but with no markers.
    What am I doing wrong?

  44. Adrian says:

    it doesn’t work as expected.
    This tag just shows an empty map without any markers on my search page.

  45. Adrian says:

    ok, I finally managed to get it to work with the wpgeo_map(), but I found another problem…
    Every quote in the marker title is converted to ‘&#8220’ and ‘&#8221’ and I have no idea why?

  46. Clay says:

    I was also using if ( isset($wpgeo) ) $wpgeo->categoryMap(); … no longer displays anything on search results after upgrading so I tried wpgeo_map() and get a map with all markers. How can I get the same result as I had before where just the markers from the search results display? Such as the wp geo contextual map shows.

  47. Adrian says:

    Now, I’am using this and it is working:

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $custom_query = array(
    'posts_per_page' => 10,
    'paged' => $paged
    );
    $newquery = array_merge( $wp_query->query, $custom_query );

    wpgeo_map( $newquery );

  48. Fantax says:

    Hello everyone,
    I read a lot of comments with same problems using the map with custom post type and taxonomies.
    This is my solution:

    'your_custom_post_type',
    'posts_per_page' => '-1',
    'tax_query' => array(
    array(
    'taxonomy' => 'your_custom_taxomony',
    'field' => 'id',
    'terms' => array(5,6),
    )
    )
    );

    if ( isset($wpgeo) ) wpgeo_map($args);
    ?>

    Attention, call to the function “wpgeo_map” out of the wordpress loop.
    I hope it was helpful.
    greetings

Leave a Reply