Skip Navigation

[Resolved] Different Custom Marker in a map based on a custom taxonomy

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Map View that includes several Markers. I would like to show different custom marker icons depending on the terms assigned to each post. If one term is assigned, use a custom Marker image specific to that term. If more than one term is assigned, use a custom "multiple term" Marker.

Solution: Add the following custom shortcode to determine how many terms are assigned to the current post in the loop:

function ts_get_post_tax_term_count_func( $atts ) {
 
  $a = shortcode_atts( array(
      'postid' => '0',
      'taxonomy' => null ), $atts
  );
 
  $terms = wp_get_post_terms( $a['postid'], $a['taxonomy']);
  return is_wp_error( $terms ) ? 0 : sizeof($terms);
}
 
add_shortcode( 'ts_get_post_tax_term_count', 'ts_get_post_tax_term_count_func');

Go to Toolset > Settings > Frontend content, and register both "has_term" and "ts_get_post_tax_term_count_func" to be used in conditionals and in 3rd party shortcode arguments.

Then use the custom shortcode in combination with has_term to display the proper markers:

Multiple types:
[wpv-conditional if="('[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']' gt '1')"]https://www.tripup.it/wp-content/plugins/toolset-maps/resources/images/markers/MULTIPLE-MARKER.png[/wpv-conditional]
 
Only one type - arte-e-intrattenimento:
[wpv-conditional if="('[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']' eq '1') AND (has_term('arte-e-intrattenimento', 'facebook-type', null) eq '1')"]https://www.tripup.it/wp-content/uploads/2018/02/marker_shopping_web_white.png[/wpv-conditional]
 
Only one type - ospitelita:
[wpv-conditional if="('[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']' eq '1') AND (has_term('ospitelita', 'facebook-type', null) eq '1')"]https://www.tripup.it/wp-content/uploads/2018/02/OSPITELITA-MARKER.png[/wpv-conditional]
 
... copy and paste the block above for each facebook-type...
 
No types:
[wpv-conditional if="( [ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']  eq '0')"]https://www.tripup.it/wp-content/plugins/toolset-maps/resources/images/markers/default.png[/wpv-conditional]

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/has_term
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/display-on-google-maps/#displaying-markers

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#626994

I am trying to:

show a different marker on map based on a custom taxonomy called "facebook-type". I did it following the link below:
https://toolset.com/forums/topic/different-marker-image-in-a-single-map/

I tried also to use a single image in a content template called "marker personalizzato" and it work, for example I remove conditional shortcode and use image url instead, for all Halt Item.

In this case the content template worked well.

Link to a page where the issue can be seen:

hidden link

I expected to see:

differents map marker

Instead, I got:
no marker.

When I try to use conditional shortcode to select different images, it doesn't show any marker on the map.

I think that "Halts" item association with "Facebook Types" taxonomy has a issue.

#627106
Screen Shot 2018-03-21 at 4.11.29 PM.png

Hi the main issue was that the function "has_term" was not registered for use in conditionals. I added it in Toolset > Settings > Frontend Content. See the screenshot.

If you want to test it out, edit this post:
https://www.tripup.it/wp-admin/post.php?post=929&action=edit
Add the "arte-e-intrattenimento" term to this post and refresh the map.

Here's the View loop code:

[wpv-layout-start]
	[wpv-items-found]
[wpv-map-render map_id="map-multi" map_height="400px"][/wpv-map-render]
	<!-- wpv-loop-start -->
	<wpv-loop>
      [wpv-map-marker map_id="map-multi" marker_id="marker-[wpv-post-id]" marker_icon="[wpv-post-body view_template='marker personalizzato']" marker_title='[types field="order"][/types] - [wpv-post-title]' address="[types field='accomodation-address' id='$accomodation'][/types]" ]<p><b>[types field='order'][/types] - [wpv-post-title]</b><br><b><i class="fa fa-calendar" aria-hidden="true"></i> </b> [wpv-conditional if="( !empty($(wpcf-date)) )"][types field='date'][/types] - [/wpv-conditional] [types field='time'][/types]
                  <br><b><i class="fa fa-clock-o" aria-hidden="true"></i> </b> [types field='halt-duration'][/types]</p>[/wpv-map-marker]
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Here's the updated Content Template code:

[wpv-conditional if="(has_term('arte-e-intrattenimento', 'facebook-type', null) eq '1')"]<em><u>hidden link</u></em>
[wpv-conditional if="(has_term('arte-e-intrattenimento', 'facebook-type', null) ne '1')"]<em><u>hidden link</u></em>

You can make changes in the Content Template to modify the icons for each term.

#627237
Schermata 2018-03-22 alle 09.49.54.png

Good morning Christian and thanks a lot for your help that has resolved the issue.

Moreover if I wanted to visualize a multi-category marker if the post (tour) had been assigned to more categories what would be the solution?

You can see "Arte, terme e gusto nel cuore dell'Abruzzo" post and relative facebook-type's category assignment as reported in the attached image.

#627420

Sorry, I do not understand the question. If the Halts post has Facebook Types "A", "B", and "C", will the same icon image be used for another Halts post that has Facebook Types "B", "C", and "D"?

Or, will different images be used for each possible combination?

#627477

Exactly, the two post will have the same icon because both has more than one category assigned.
Best regards,
Danilo.

#627523

You can try, but it becomes quite complex because of all the possible options. Here's a custom shortcode that will tell you how many terms from a single taxonomy are applied to any post. Add this code in functions.php:

function ts_get_post_tax_term_count_func( $atts ) {

  $a = shortcode_atts( array(
      'postid' => '0',
      'taxonomy' => null ), $atts
  );

  $terms = wp_get_post_terms( $a['postid'], $a['taxonomy']);
  return is_wp_error( $terms ) ? 0 : sizeof($terms);
}

add_shortcode( 'ts_get_post_tax_term_count', 'ts_get_post_tax_term_count_func');

Go to Toolset > Settings > Frontend Content and enter "ts_get_post_tax_term_count" without the quotation marks in Third-party shortcode arguments.

Now place the shortcode in the Loop Output of the View, and it should output the number of terms from the facebook-type taxonomy for each post:

[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']

You can try using that shortcode in a conditional, but the conditional is complex because of all the possible variations. You must first check if the number of terms is greater than 1. If so, show the multi-type marker. If the number is 1, then you must also know which term it is. That combines two conditions, and must be repeated for each type of icon. Then finally show a default icon for zero facebook-type terms. Try something like this:

Multiple types:
[wpv-conditional if="('[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']' gt '1')"]<em><u>hidden link</u></em>

Only one type - arte-e-intrattenimento:
[wpv-conditional if="('[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']' eq '1') AND (has_term('arte-e-intrattenimento', 'facebook-type', null) eq '1')"]<em><u>hidden link</u></em>

Only one type - ospitelita:
[wpv-conditional if="('[ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']' eq '1') AND (has_term('ospitelita', 'facebook-type', null) eq '1')"]<em><u>hidden link</u></em>

... copy and paste the block above for each facebook-type...

No types:
[wpv-conditional if="( [ts_get_post_tax_term_count postid='[wpv-post-id]' taxonomy='facebook-type']  eq '0')"]<em><u>hidden link</u></em>