Categories

Peer Categories

Author: Scott Reilly
Version: 2.2
First released: 2009-06-12
Last update: 2021-10-24
Compatibility: WP 4.6 – 5.8.6
Download: [ zip ]
Description:

List the categories that are peer (i.e. share the same category parent) to all lowest-level…

Extended Description

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 | Plugin Directory Page | GitHub | Author Homepage

Developer Documentation

Developer documentation can be found in 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.

Find out more at the plugin’s WordPress Plugin Repository page.

Installation

  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”

Release Log

2.2 (2021-10-23)

  • Change: Remove long-deprecated functions peer_categories(), get_peer_categories_list(), and get_peer_categories()
  • New: Add DEVELOPER-DOCS.md and move template tag and hooks documentation into it
  • Change: Tweak installation instruction
  • Change: Note compatibility through WP 5.8+
  • Change: Pare down tags in readme.txt header
  • Unit tests:
    • Change: Restructure unit test directories
      • Change: Move phpunit/ into tests/phpunit/
      • Change: Move phpunit/bin/ into tests/
    • Change: Remove ‘test-‘ prefix from unit test file
    • Change: In bootstrap, store path to plugin file constant
    • Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

2.1.5 (2021-04-17)

  • Change: Note compatibility through WP 5.7+
  • Change: Update copyright date (2021)

2.1.4 (2020-09-05)

  • Change: Restructure unit test file structure
    • New: Create new subdirectory phpunit/ to house all files related to unit testing
    • Change: Move bin/ to phpunit/bin/
    • Change: Move tests/bootstrap.php to phpunit/
    • Change: Move tests/ to phpunit/tests/
    • Change: Rename phpunit.xml to phpunit.xml.dist per best practices
  • Change: Note compatibility through WP 5.5+

Copyright & Disclaimer

Copyright © 2009-2022 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s WordPress.org support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.