Make the Store Thumbnail Clickable

You can turn the store thumbnail in the search results into a link in the Section Editor, or with help from the wpsl_listing_template filter.

The thumbnail links to the store page when “Create landing pages?” is enabled under Store Locator > Settings > Local Pages. Otherwise it links to the URL from the “Additional Information” tab. Stores without a thumbnail or without a link show the same as before.

Section Editor

  1. Go to Store Locator > Settings > Fields Manager > Section Editor, select the List section and click Load.
  2. Find <%= typeof thumb !== "undefined" ? thumb : "" %> near the top of the code, and replace it with:
    <% var thumbLink = typeof url !== "undefined" ? url : ""; %>
    <% if ( typeof permalink !== "undefined" && permalink ) { thumbLink = permalink; } %>
    <% if ( typeof thumb !== "undefined" && thumb && thumbLink ) { %>
    <a href="<%= thumbLink %>"><%= thumb %></a>
    <% } else { %>
    <%= typeof thumb !== "undefined" ? thumb : "" %>
    <% } %>

    To open the link in a new window, add target="_blank" rel="noopener" to the a tag.

  3. Check Activate this custom template section and click Save Section.

Search Results Template

With the filter, the link follows “Open links in a new window?”, and the rest of the template keeps following your settings. If a custom List section is activated in the Section Editor, the plugin uses that saved code and this filter doesn’t run for it.

add_filter( 'wpsl_listing_template', 'custom_listing_template_clickable_thumb' );
function custom_listing_template_clickable_thumb( $listing_template ) {
$sections = wpsl_get_service( 'template_sections' );
$settings = wpsl_get_service( 'wpsl_settings' );
// Link to the store page when permalinks are enabled, otherwise to the store URL.
$field = $settings->get( 'local_pages', 'permalinks' ) ? 'permalink' : 'url';
$new_window = $sections->new_window();
// The thumbnail placeholder in the default template.
$thumb = '<%= typeof thumb !== "undefined" ? thumb : "" %>';
// Only add the link when the store has a thumbnail and a link target.
$has_thumb = 'typeof thumb !== "undefined" && thumb';
$has_link = 'typeof ' . $field . ' !== "undefined" && ' . $field;
$linked_thumb = '<% if ( ' . $has_thumb . ' && ' . $has_link . ' ) { %>';
$linked_thumb .= '<a href="<%= ' . $field . ' %>"' . $new_window . '>' . $thumb . '</a>';
$linked_thumb .= '<% } else { %>' . $thumb . '<% } %>';
$listing_template = str_replace( $thumb, $linked_thumb, $listing_template );
return $listing_template;
}
Place this code in the functions.php in your theme folder.