Peer Categories

Author:Scott Reilly
Version:1.0
Last update:10 Apr 2009
Compatibility:WP 2.5+, 2.6+, 2.7+
Comments:go here
Download:[ zip ]
Description:

Display only the categories that are peer (i.e. share the same category parent) to all lowest-level assigned categories for the specified post.

Extended Description

Display only the categories that are peer (i.e. share the same category parent) to all lowest-level assigned categories for the specified post.

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. 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 argment to the template tag allows you to define a custom separator, e.g. to have a simple comma-separated list of peer categories: <?php 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 preceeding example):

<?php peer_categories(); ?>
Displays something like:

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

<?php peer_categories(','); ?></ul>
Displays something like:
<a href="http://yourblog.com/category/fruiting/bell-pepper">Bell Pepper</a>, <a href="http://yourblog.com/category/fruiting/cucumber">Cucumber</a>, <a href="http://yourblog.com/category/fruiting/pumpkin">Pumpkin</a>

Read the readme.txt.


Installation

  1. Unzip peer-categories-v1.0.zip inside the /wp-content/plugins/ directory for your site
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. (optional) Add filters for ‘peer_categories’ to filter peer category listing
  4. Use the template tag <?php peer_categories(); ?> somewhere inside “the loop”

Template Tag

This plugin provides 3 template tag(s).

Function

peer_categories( $separator, $post_id = false )

Displays the return value of get_peer_categories_list(). This is most likely the tag you will want to use in your templates.

Arguments

  • $separator : (optional) String to use as separator between listed categories. If blank or not specified, the categories will be displayed as an HTML list.
  • $post_id : (optional) ID of the post to retrieve categories; if false or not specified, then the current (‘in the loop’) post will be used.

Function

get_peer_categories_list( $separator, $post_id = false )

Retrieves and formats for display (but doesn’t actually display) the list of categories peer to all assigned categories for the specified post.

Arguments

  • $separator : (optional) String to use as separator between listed categories. If blank or not specified, the categories will be displayed as an HTML list.
  • $post_id : (optional) ID of the post to retrieve categories; if false or not specified, then the current (‘in the loop’) post will be used.

Function

get_peer_categories( $id = false )

Utility function to retrieve the categories peer to all assigned categories for the specified post.

Arguments

  • $id : (optional) ID of the post to retrieve categories; if false or not specified, then the current (‘in the loop’) post will be used.

Frequently Asked Questions

  • Q: Why isn’t an assigned category for the post showing up in the ‘peer_categories()’ listing?

    A: 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.


Release Log


Copyright & Disclaimer

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

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.