php - 使用 Ajax 更改 WordPress 页面上的简码 ID
问题描述
我在 WordPress 中有一个自定义 PHP 页面,需要显示连接到地图插件简码的按钮。每个按钮都需要更改页面上显示的地图。Ajax 操作正在运行,但仅显示地图的 ID,而不是完整的简码。
我在 StackOverflow 上找到了一个类似的帖子,对 Contact Form 7 短代码做了类似的事情。
使用的Javascript:
<script type="text/javascript">
$(function () {
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
$('.button-map').click(function () {
$.ajax({
url: ajaxurl, //global variable provided by WP
type: 'GET',
data: {
'action': 'get_map_frame',
'map_id': $(this).data('id')
},
success: function (data) {
$('#map_area').html(data);
}
});
});
});
</script>
functions.php 中的 Ajax 调用:
add_action('wp_ajax_get_map_frame', 'get_map_frame');
add_action('wp_ajax_nopriv_get_map_frame', 'get_map_frame' );
function get_map_frame() {
if (isset($_GET['map_id'])) {
$map_id = (int) $_GET['map_id'];
echo do_shortcode('[cspm_main_map id="' . $map_id . '"]');
}
exit();
}
页面代码 HTML:
<div class="w-col w-col-12">
<div class="column-29 w-col w-col-3">
<div><a duplicate="fp-link-1" href="#" class="button-map stu w-button" data-id="5425">Dining and Drinks</a>
</div>
</div>
<div class="column-30 w-col w-col-3">
<div>
<a duplicate="fp-link-1" href="#" class="button-map stu w-button" data-id="5695">Entertainment and Transportation</a>
</div>
</div>
<div class="column-30 w-col w-col-3">
<div>
<a duplicate="fp-link-1" href="#" class="button-map stu w-button" data-id="5697">Shopping</a>
</div>
</div>
<div class="column-30 w-col w-col-3">
<div>
<a duplicate="fp-link-1" href="#" class="button-map stu w-button" data-id="5698">Pets and Parks</a>
</div>
</div>
<div id="map_area"></div>
</div>
我想要实现的是每个按钮都会拉出与地图 ID 关联的单独地图。当前,当单击该按钮时,它仅返回页面上的实际地图 ID,而不返回用于显示预期地图的其余短代码。
解决方案
推荐阅读
- google-earth-engine - Google Earth Engine中栅格的比例值
- java - Java 并发实践图 12.1 TimedPutTakeTest
- java - 在 AWS Elastic Beanstack 上部署 Node.J 应用程序时出错
- php - 如何在 php/html 的搜索结果中添加复选框
- .htaccess - 强制重定向到 HTTPS,除了一个带有 .htaccess 的目录?
- regression - Julia:如何使用 FixedEffectModels 包获得回归的残差
- python - 使用“deepcopy”后的意外行为
- vba - 如何使用 VBA 中组合框的变量分别寻址单元格编号
- html - 如何在烧瓶模板中添加引导字形图标?
- ios - 将图片添加到json?