Title: Peer Categories
Author: Scott Reilly
Published: <strong>2009 年 6 月 12 日</strong>
Last modified: 2025 年 4 月 21 日

---

搜索插件

![](https://ps.w.org/peer-categories/assets/banner-772x250.png?rev=835972)

![](https://ps.w.org/peer-categories/assets/icon-128x128.png?rev=976260)

# Peer Categories

 作者：[Scott Reilly](https://profiles.wordpress.org/coffee2code/)

[下载](https://downloads.wordpress.org/plugin/peer-categories.2.3.1.zip)

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

 [支持](https://wordpress.org/support/plugin/peer-categories/)

## 描述

This plugin provides a template tag which acts a modified version of WordPress’s
built-in template tag, `the_category()`. `the_category()` lists all categories directly
assigned to the specified post. `c2c_peer_categories()` lists those categories _PLUS_
any categories that are peer to those categories and _MINUS_ categories that are
parents to other assigned categories. Peer categories are categories that all share
the same category parent.

For example, assume your category structure is hierarchical and looks like this:

    ```
    Vegetables
    |-- Leafy
    |   |-- Broccoli
    |   |-- Bok Choy
    |   |-- Celery
    |-- Fruiting
    |   |-- Bell Pepper
    |   |-- Cucumber
    |   |-- Pumpkin
    |-- Podded
    |   |-- Chickpea
    |   |-- Lentil
    |   |-- Soybean
    ```

If you directly assigned the categories “Fruiting” and “Pumpkin” to a post, `peer_categories()`
would return a list that consists of: “Bell Pepper”, “Cucumber”, and “Pumpkin”. 
Notice that since “Fruiting” was a parent to a directly assigned category, it and
its peers are not included in the list. If only “Fruiting” were selected as a category,
then “Leafy”, “Fruiting”, and “Podded” would have been listed.

By default, categories are listed as an HTML list. The first argument to the template
tag allows you to define a custom separator, e.g. to have a simple comma-separated
list of peer categories: `<?php c2c_peer_categories(', '); ?>`.

As with categories listed via `the_category()`, categories that are listed are presented
as links to the respective category’s archive page.

Example usage (based on preceding example):

 * `<?php c2c_peer_categories(); ?>`

Outputs something like:

    ```
    <ul><li><a href="https://example.com/category/fruiting/bell-pepper">Bell Pepper</a></li>
    <li><a href="https://example.com/category/fruiting/cucumber">Cucumber</a></li>
    <li><a href="https://example.com/category/fruiting/pumpkin">Pumpkin</a></li></ul>
    ```

 * `<?php c2c_peer_categories( ', ' ); ?></ul>`

Outputs something like:

    ```
    <a href="https://example.com/category/fruiting/bell-pepper">Bell Pepper</a>, <a href="https://example.com/category/fruiting/cucumber">Cucumber</a>, <a href="https://example.com/category/fruiting/pumpkin">Pumpkin</a>
    ```

Links: [Plugin Homepage](https://coffee2code.com/wp-plugins/peer-categories/) | 
[Plugin Directory Page](https://wordpress.org/plugins/peer-categories/) | [GitHub](https://github.com/coffee2code/peer-categories/)
| [Author Homepage](https://coffee2code.com)

### Developer Documentation

Developer documentation can be found in [DEVELOPER-DOCS.md](https://github.com/coffee2code/peer-categories/blob/master/DEVELOPER-DOCS.md).
That documentation covers the template tags and hooks provided by the plugin.

As an overview, these are the template tags provided by the plugin:

 * `c2c_peer_categories()` : Outputs the peer categories.
 * `c2c_get_peer_categories_list()` : Returns the list of peer categories.
 * `c2c_get_peer_categories()` : Returns the list of peer categories for the specified
   post.

These are the hooks provided by the plugin:

 * `c2c_peer_categories` _(action)_, `c2c_get_peer_categories_list`, `c2c_get_peer_categories`_(
   filters)_ :
    Allows for an alternative approach to safely invoke each of the 
   identically named functions in such a way that if the plugin were deactivated
   or deleted, then your calls to the functions won’t cause errors on your site.
 * `c2c_peer_categories_list` _(filter)_ :
    Customizes the return value of the `
   c2c_peer_categories_list()` function.
 * `c2c_get_peer_categories_omit_ancestors` _(filter)_ :
    Customizes the function
   argument indicating if ancestor categories of all directly assigned categories(
   even if directly assigned themselves) should be omitted from the return list 
   of categories.

## 安装

 1. Install via the built-in WordPress plugin installer. Or install the plugin code
    inside the plugins directory for your site (typically `/wp-content/plugins/`).
 2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
 3. Optional: Add filters for ‘c2c_peer_categories_list’ to filter peer category listing
 4. Use the template tag `<?php c2c_peer_categories(); ?>` in a theme template somewhere
    inside “the loop”

## 常见问题

### Why isn’t an assigned category for the post showing up in the ‘c2c_peer_categories()’ listing?

If an assigned category is the parent for one or more other assigned categories 
for the post, then the category parent is not included in the listing. Only peers
to the lowest-level assigned categories are considered.

### Does this plugin have unit tests?

Yes. The tests are not packaged in the release .zip file or included in plugins.
svn.wordpress.org, but can be found in the [plugin’s GitHub repository](https://github.com/coffee2code/peer-categories/).

## 评价

此插件暂无评价。

## 贡献者及开发者

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

贡献者

 *   [ Scott Reilly ](https://profiles.wordpress.org/coffee2code/)

[帮助将「Peer Categories」翻译成简体中文。](https://translate.wordpress.org/projects/wp-plugins/peer-categories)

### 对开发感兴趣吗?

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

## 更新日志

#### 2.3.1 (2025-04-21)

 * Change: Note compatibility through WP 6.8+
 * Change: Note compatibility through PHP 8.3+
 * Change: Update copyright date (2025)
 * Unit tests:
    - Change: Explicitly define return type for overridden method

#### 2.3 (2024-08-28)

Highlights:

This minor release prevents translations from containing unintended markup, removes
unnecessary functionality within `c2c_get_peer_categories()`, notes compatibility
through WP 6.6+, removes unit tests from release packaging, and updates copyright
date (2024).

Details:

 * Change: Remove behavior of `c2c_get_peer_categories()` potentially returning 
   all top-level categories
 * Change: Prevent translations from containing unintended markup
 * Change: Add missing inline comment for translators
 * Change: Note compatibility through WP 6.6+
 * Change: Add missing inline documentation for a filter argument
 * Change: Update copyright date (2024)
 * Change: Remove development and testing-related files from release packaging
 * New: Add `.gitignore` file
 * Unit tests:
    - Allow tests to run against current versions of WordPress
    - New: Add `composer.json` for PHPUnit Polyfill dependency
    - Hardening: Prevent direct web access to `bootstrap.php`
    - Change: In bootstrap, store path to plugin directory in a constant
    - Change: Prevent PHP warnings due to missing core-related generated files
    - New: Add tests for `c2c_peer_categories()`
    - New: Add tests for function invocation filters
    - Change: Tweak some inline comment formatting

#### 2.2.1 (2023-05-20)

 * Change: Note compatibility through WP 6.3+
 * Change: Update copyright date (2023)

_Full changelog is available in [CHANGELOG.md](https://github.com/coffee2code/peer-categories/blob/master/CHANGELOG.md)._

## 额外信息

 *  版本 **2.3.1**
 *  最后更新：**1 年前**
 *  活跃安装数量 **10+**
 *  WordPress 版本 ** 4.6 或更高版本 **
 *  已测试的最高版本为 **6.8.5**
 *  语言
 * [English (US)](https://wordpress.org/plugins/peer-categories/)
 * 标签
 * [category](https://cn.wordpress.org/plugins/tags/category/)[coffee2code](https://cn.wordpress.org/plugins/tags/coffee2code/)
   [related posts](https://cn.wordpress.org/plugins/tags/related-posts/)[the_category](https://cn.wordpress.org/plugins/tags/the_category/)
 *  [高级视图](https://cn.wordpress.org/plugins/peer-categories/advanced/)

## 评级

尚未提交反馈。

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

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

## 贡献者

 *   [ Scott Reilly ](https://profiles.wordpress.org/coffee2code/)

## 支持

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

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

## 捐助

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

 [ 捐助此插件 ](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6ARCFJ9TX3522)