Parentless Categories

plugin banner

Version: 1.1.4

Like the_category(), list categories assigned to a post, but excluding assigned categories that have a child category also assigned to

Author:Scott Reilly (profile at wordpress.org)
WordPress version required:4.6
WordPress version tested:6.3
Plugin version:2.2.1
Added to WordPress repository:12-06-2009
Last updated:22-08-2023
Rating, %:0
Rated by:0
Plugin URI:https://coffee2code.com/wp-plugins/parentless...
Total downloads:5 049
Active installs:10+
plugin download
Click to start download

This plugin provides a template tag which acts as a modified version of WordPress’s built-in template tag, the_category(). the_category() lists all categories directly assigned to the specified post. c2c_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
|   |-- Soybean

If you directly assigned the categories “Fruiting”, “Cucumber”, and “Pumpkin” to a post, c2c_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 argument to the template tag allows you to define a custom separator, e.g. to have a simple comma-separated list of categories: <?php c2c_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 preceding example):

  • <?php c2c_parentless_categories(); ?>

Outputs 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 c2c_parentless_categories( ',' ); ?></ul>

Outputs something like:

<a href="http://yourblog.com/category/fruiting/cucumber">Cucumber</a>, <a href="http://yourblog.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_parentless_categories() : Outputs the parentless categories.
  • c2c_get_parentless_categories_list() : Returns the list of parentless categories.
  • c2c_get_parentless_categories() : Returns the list of parentless categories for the specified post.

These are the hooks provided by the plugin:

  • c2c_parentless_categories (action), c2c_get_parentless_categories_list, c2c_get_parentless_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_parentless_categories_list (filter) :
    Customizes the return value of the c2c_parentless_categories_list() function.
  • c2c_get_parentless_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.

FAQ
ChangeLog