R
R
Roman Filippov2018-10-05 11:24:40
Yandex maps
Roman Filippov, 2018-10-05 11:24:40

How to display labels on the map for the current category?

Products have an arbitrary ACF field with coordinates for the Yandex Map for ACF plugin, on the archives page this map is displayed with all existing tags.
There is a code that displays the center of the map and its scale, you need to make this code dependent on the category (its id), that is, so that such and such a center with such and such a scale is displayed for such and such a category id.
In fact, you need to set a condition, but I don’t know how to write it ...

// Карта Яндекс
add_theme_support( 'post-thumbnails' );
function action_function_name_6515(){
    global $wpdb; 
    $products = $wpdb->get_results("SELECT ID FROM `ev_posts` WHERE `post_type`='product' AND `post_status`='publish'");
    $result = array();
    foreach($products as $product){
        $result[] = $product->ID;
    }
    the_multiple_yandex_map($result);
}

function the_multiple_yandex_map($post_ids = array())
{
    $marks = array(); // Все метки
    $marks_counter = 1;

    foreach ($post_ids as $post_id) {
        $data = (array)json_decode(get_field('halls_mapya', $post_id, false), true); // загрузка меток из записи
        if (!isset($data['marks']) || !is_array($data['marks'])) {
            continue;
        }

        foreach ((array)$data['marks'] as $mark) {
            $mark['id'] = $marks_counter;
      $mark['content'].='<a href="'.get_permalink($post_id).'">'.get_the_title($post_id).'</a>';
      $mark['content'].=get_the_post_thumbnail($post_id);
            $marks[] = $mark;

            $marks_counter++;
        }
    }

    $map_data = (array)json_decode(get_field('halls_mapya', get_the_ID(), false), true); // загрузка параметров карты для вывода всех меток
  $map_data = array(
        "center_lat" => 55.755456,
        "center_lng" => 37.623104,
        "zoom" => 10,
        "type" => "map"
    ); // загрузка параметров карты для вывода всех меток
    $map_data['marks'] = $marks;
  
    the_yandex_map('', false, json_encode($map_data), 450);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Filippov, 2018-10-05
@Webram

While writing the question, I came up with a solution:

if(is_product_category('61')) {
        $map_data = array(
        "center_lat" => 15.755456,
        "center_lng" => 37.623104,
        "zoom" => 10,
        "type" => "map"       
    );       
    }else{
        $map_data = array(
        "center_lat" => 55.755456,
        "center_lng" => 37.623104,
        "zoom" => 10,
        "type" => "map"     
    );
    }
    $map_data['marks'] = $marks;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question