Title: Template Map
Author: Jon Christopher
Published: <strong>2014 年 7 月 18 日</strong>
Last modified: 2022 年 2 月 10 日

---

搜索插件

![](https://ps.w.org/template-map/assets/banner-772x250.jpg?rev=951721)

**该插件尚未通过WordPress的最新3个主要版本进行测试**。 当与较新版本的WordPress一起
使用时，可能不再受到维护或支持，并且可能会存在兼容性问题。

![](https://s.w.org/plugins/geopattern-icon/template-map_d59f6f.svg)

# Template Map

 作者：[Jon Christopher](https://profiles.wordpress.org/jchristopher/)

[下载](https://downloads.wordpress.org/plugin/template-map.1.0.1.zip)

 * [详情](https://cn.wordpress.org/plugins/template-map/#description)
 * [评价](https://cn.wordpress.org/plugins/template-map/#reviews)
 *  [安装](https://cn.wordpress.org/plugins/template-map/#installation)
 * [开发进展](https://cn.wordpress.org/plugins/template-map/#developers)

 [支持](https://wordpress.org/support/plugin/template-map/)

## 描述

Template Map is a utility plugin designed to make the creation and management of
navigation elements in your custom theme that much easier. It abstracts the need
to hard code post IDs and the like from your global and header navigation systems(
if you choose to not use Menus) for instance.

#### For Example

Many times you may use something like the following to build your main site navigation:

    ```
    <nav>
      <ul>
        <li>
          <a href="<?php echo get_permalink( 83 ); ?>">About</a>
        </li>
      </ul>
    </nav>
    ```

That works great during initial development, and continues to hold up when you migrate
to production en masse for the first time. But what about when you’re making subsequent
updates and the post IDs in your development environment are different than those
in production? You could take the time to sync the databases each and every time,
but that’s a bit overkill. Here’s where Template Map comes in to play:

    ```
    <nav>
      <ul>
        <li>
          <?php $about_page_id = TemplateMap()->get_id_from_template( 'template-about.php' ); ?>
          <a href="<?php echo get_permalink( $about_page_id ); ?>">About</a>
        </li>
      </ul>
    </nav>
    ```

Template Map allows you to _dynamically_ retreive your desired post ID based on 
the Page Template you told it to use. Page Template filenames rarely (if ever) change,
so it’s the core concept of Template Map’s implementation. Everything works backwards
from here.

Naturally this philosophy assumes your Page Template is used only once, it’s important
to keep that in mind. Modern client sites often use unique Page Templates for the‘
parent’ pages of each site section.

#### Current Site Section

Template Map also makes it easier to determine whether the current page is within
a site ‘section’ which is defined as a top level Page with a unique Page Template
that has any number of child Pages and/or Custom Post Types within it. This is very
useful when trying to set a ‘current’ state in your navigation, for example:

    ```
    <nav>
      <ul>
        <li class="<?php if( TemplateMap()->maybe_in_section( 'template-about.php' ) ) : ?> current<?php endif; ?>">
          <?php $about_page_id = TemplateMap()->get_id_from_template( 'template-about.php' ); ?>
          <a href="<?php echo get_permalink( $about_page_id ); ?>">About</a>
        </li>
      </ul>
    </nav>
    ```

Based on the same principle you can use this utility method to properly orient yourself
when outputting conditional classes contingent on the current page being within 
a ‘secton’ on your site.

## 安装

 1. Upload `templatemap` to the `/wp-content/plugins/` directory
 2. 通过WordPress的的“Plugins”菜单激活插件
 3. Replace calls to `get_permalink( 83 )` with `get_permalink( TemplateMap()->get_id_from_template('
    template-about.php' ) )` where `template-about.php` is the Page you wish to link

## 常见问题

### How do I define Custom Post Types within a section?

There’s a filter for that. `template_map_post_types` accepts two parameters, the
second of which is the Page Template filename in question. You can conditionally
return an array of CPT names that will be utilized when checking to see whether 
the current page is within the section in question.

## 评价

此插件暂无评价。

## 贡献者及开发者

「Template Map」是开源软件。 以下人员对此插件做出了贡献。

贡献者

 *   [ Jon Christopher ](https://profiles.wordpress.org/jchristopher/)

[帮助将「Template Map」翻译成简体中文。](https://translate.wordpress.org/projects/wp-plugins/template-map)

### 对开发感兴趣吗?

您可以[浏览代码](https://plugins.trac.wordpress.org/browser/template-map/)，查看
[SVN仓库](https://plugins.svn.wordpress.org/template-map/)，或通过[RSS](https://plugins.trac.wordpress.org/log/template-map/?limit=100&mode=stop_on_copy&format=rss)
订阅[开发日志](https://plugins.trac.wordpress.org/log/template-map/)。

## 更新日志

#### 1.0.1

 * When using maybe_in_section you can pass a 2nd parameter to force a parent ID

#### 1.0

 * Initial release

## 额外信息

 *  版本 **1.0.1**
 *  最后更新：**4 年前**
 *  活跃安装数量 **10+**
 *  WordPress 版本 ** 3.9 或更高版本 **
 *  已测试的最高版本为 **5.9.0**
 *  语言
 * [English (US)](https://wordpress.org/plugins/template-map/)
 * 标签
 * [link](https://cn.wordpress.org/plugins/tags/link/)[template](https://cn.wordpress.org/plugins/tags/template/)
 *  [高级视图](https://cn.wordpress.org/plugins/template-map/advanced/)

## 评级

尚未提交反馈。

[Your review](https://wordpress.org/support/plugin/template-map/reviews/#new-post)

[查看全部评论](https://wordpress.org/support/plugin/template-map/reviews/)

## 贡献者

 *   [ Jon Christopher ](https://profiles.wordpress.org/jchristopher/)

## 支持

有话要说吗？是否需要帮助？

 [查看支持论坛](https://wordpress.org/support/plugin/template-map/)

## 捐助

您愿意支持这个插件的发展吗?

 [ 捐助此插件 ](http://mondaybynoon.com/donate/)