Parentless Categories
- Author:Scott Reilly
- Version:1.0
- Last update:11 Apr 2009
- Compatibility:WP 2.5+, 2.6+, 2.7+
- Comments:go here
- Download:[ zip ]
- Description:
Like the_category(), list categories assigned to a post, but excluding assigned categories that have a child category also assigned to the post.
-
Extended Description
Like the_category(), list categories assigned to a post, but excluding assigned categories that have a child category also assigned to the 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.parentless_categories()lists those categories, except for categories that are parents to other assigned categories.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
| |– SoybeanIf you directly assigned the categories “Fruiting”, “Cucumber”, and “Pumpkin” to a post,
parentless_categories()would return a list that consists of: “Cucumber”, and “Pumpkin”. Notice that since “Fruiting” was a parent to a directly assigned category, it is not included in the list.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 categories:
<?php parentless_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 parentless_categories(); ?>
Displays something like:
<ul><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 parentless_categories(','); ?></ul>
Displays something like:
<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
- Unzip
parentless-categories-v1.0.zipinside the/wp-content/plugins/directory for your site - Activate the plugin through the ‘Plugins’ admin menu in WordPress
- (optional) Add filters for ‘parentless_categories’ to filter parentless category listing
- Use the template tag
<?php parentless_categories(); ?>somewhere inside “the loop”
- Unzip
Template Tag
This plugin provides 3 template tag(s).
Function
parentless_categories( $separator, $post_id = false )Displays the return value of get_parentless_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_parentless_categories_list( $separator, $post_id = false )Retrieves and formats for display (but doesn’t actually display) the list of assigned categories for the specified post (minus alos-assigned parent categories).
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_parentless_categories( $id = false )Utility function to retrieve the assigned categories for the specified post (minus alos-assigned parent categories).
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 ‘parentless_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.
-
Release Log
- 11 Apr 2009 : v1.0 — Initial release
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.
