Drupal 7:如何在node--page模板展示分类法内容并实现地图与列表
Drupal 7:基于分类术语渲染地点地图与列表解决方案
嘿,针对你在Drupal 7里基于markets分类术语(比如East Coast/West Coast)渲染对应地点节点的地图和列表需求,我整理了一套实操方案,一步步来搞定它!
一、核心思路
我们要在taxonomy-term--markets.tpl.php模板中,先获取当前分类下的所有已发布地点节点,再分别渲染列表和地图内容。推荐用预处理函数来准备变量(更符合Drupal最佳实践),再在模板里调用变量渲染。
二、步骤1:编写预处理函数准备节点数据
在你的主题template.php文件中添加以下代码,用来查询当前分类下的地点节点:
function YOUR_THEME_preprocess_taxonomy_term(&$variables) { // 仅针对markets分类术语模板生效 if ($variables['vocabulary_machine_name'] == 'markets') { $current_term_id = $variables['term']->tid; // 构建查询:获取属于当前分类的已发布地点节点(替换为你的内容类型和字段机器名) $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'location') // 你的地点内容类型机器名 ->propertyCondition('status', 1) // 只取已发布节点 ->fieldCondition('field_market', 'tid', $current_term_id); // 你的分类字段机器名 $result = $query->execute(); // 将加载好的节点赋值给模板变量 $variables['location_nodes'] = isset($result['node']) ? node_load_multiple(array_keys($result['node'])) : []; } }
⚠️ 注意:替换代码中的YOUR_THEME为你的主题机器名,location为你的地点内容类型机器名,field_market为你的分类字段机器名!
三、步骤2:在模板中渲染地点列表
在taxonomy-term--markets.tpl.php中添加以下代码,渲染带详情的地点列表:
<?php if (!empty($location_nodes)): ?> <div class="location-list"> <h2><?php print t('该区域的地点列表'); ?></h2> <ul> <?php foreach ($location_nodes as $node): ?> <?php // 提取各字段值(替换为你的实际字段机器名) $title = $node->title; // 地址字段(假设是文本字段或地址字段) $address_items = field_get_items('node', $node, 'field_location_address'); $address = $address_items ? render(field_view_value('node', $node, 'field_location_address', $address_items[0])) : t('暂无地址'); // 电话号码字段 $phone_items = field_get_items('node', $node, 'field_phone'); $phone = $phone_items ? $phone_items[0]['value'] : t('暂无电话'); // 营业时间字段 $hours_items = field_get_items('node', $node, 'field_business_hours'); $hours = $hours_items ? render(field_view_value('node', $node, 'field_business_hours', $hours_items[0])) : t('暂无营业时间'); ?> <li class="location-item"> <h3><?php print l($title, 'node/' . $node->nid); ?></h3> <p class="location-address"><?php print $address; ?></p> <p class="location-phone"><?php print t('电话: @phone', ['@phone' => $phone]); ?></p> <p class="location-hours"><?php print t('营业时间: @hours', ['@hours' => $hours]); ?></p> </li> <?php endforeach; ?> </ul> </div> <?php endif; ?>
四、步骤3:在模板中渲染地图
假设你使用了Geofield模块存储地点坐标,或者Google Maps模块来生成地图,在模板中添加以下代码:
<?php if (!empty($location_nodes)): ?> <?php // 收集所有地点的坐标和信息 $map_markers = []; foreach ($location_nodes as $node): $geo_items = field_get_items('node', $node, 'field_geo_location'); // 你的Geofield字段机器名 if ($geo_items): $lat = $geo_items[0]['lat']; $lon = $geo_items[0]['lon']; $map_markers[] = [ 'latitude' => $lat, 'longitude' => $lon, 'title' => $node->title, 'text' => render(field_view_field('node', $node, 'field_location_address')) . '<br>' . t('电话: @phone', ['@phone' => $phone]), ]; endif; endforeach; ?> <?php if (!empty($map_markers)): ?> <div class="location-map"> <h2><?php print t('该区域的地点地图'); ?></h2> <?php // 使用Google Maps模块的theme函数生成地图(需先启用模块) print theme('gmap', [ 'maptype' => 'roadmap', 'zoom' => 8, 'markers' => $map_markers, 'width' => '100%', 'height' => '400px', ]); ?> </div> <?php endif; ?> <?php endif; ?>
五、额外优化建议
- 样式美化:通过添加CSS类(比如
.location-list、.location-map)来自定义列表和地图的样式,提升页面美观度 - 性能优化:如果地点节点数量较多,可以考虑使用
pager来分页,或者用Views模块来替代EntityFieldQuery(Views更灵活,还支持缓存) - 字段兼容性:如果你的地址字段用了Address Field模块,渲染方式可以调整为
render(field_view_field('node', $node, 'field_location_address'))来输出完整地址
内容的提问来源于stack exchange,提问作者Mary Warrington
相关产品推荐
相关产品推荐

