Parentless 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_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:

|-- 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="">Cucumber</a></li>
<li><a href="">Pumpkin</a></li></ul>
  • <?php c2c_parentless_categories( ',' ); ?></ul>

Outputs something like:

<a href="">Cucumber</a>, <a href="">Pumpkin</a>

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Template Tags

The plugin provides three optional template tags for use in your theme templates.


  • <?php function c2c_parentless_categories( $separator = '', $post_id = false ) ?>
    Outputs the parentless categories.

  • <?php function c2c_get_parentless_categories_list( $separator = '', $post_id = false ) ?>
    Gets the list of parentless categories.

  • <?php function c2c_get_parentless_categories( $post_id = false, $omit_ancestors = true ) ?>
    Returns the list of parentless categories for the specified post.


  • $separator
    Optional argument. (string) String to use as the separator. Default is ”, which indicates unordered list markup should be used.

  • $post_id
    Optional argument. (int) Post ID. If ‘false’, then the current post is assumed. Default is ‘false’.

  • $omit_ancestors
    Optional argument. (bool) Should any ancestor categories be omitted from being listed? If false, then only categories that are directly assigned to another directly assigned category are omitted. Default is ‘true’.


  • (See Description section)


The plugin is further customizable via five hooks. Code using these filters should ideally be put into a mu-plugin or site-specific plugin (which is beyond the scope of this readme to explain). Less ideally, you could put them in your active theme’s functions.php file.

c2c_parentless_categories (action), c2c_get_parentless_categories_list, c2c_get_parentless_categories (filters)

These actions and filters allow you to use an alternative approach to safely invoke each of the identically named function 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.


  • (see respective functions)


Instead of:

<?php c2c_parentless_categories( ',' ); ?>


<?php do_action( 'c2c_parentless_categories', ',' ); ?>

c2c_parentless_categories_list (filter)

The ‘c2c_parentless_categories_list’ filter allows you to customize or override the return value of the c2c_parentless_categories_list() function.


  • string $thelist : the generated list of categories with complete HTML markup, or __( ‘Uncategorized’ ) if the post didn’t have any categories
  • string $separator : the separator specified by the user, or ” if not specified
  • int|false $post_id : the ID of the post, or false to indicate the current post


 * Amend comma-separated parentless categories listing with a special string.
 * @param  string $thelist The parentless categories list.
 * @param  string $separator Optional. String to use as the separator.
 * @return string
function c2c_parentless_categories_list( $thelist, $separator ) {
    // If not categorized, do nothing
    if ( __( 'Uncategorized' ) == $thelist ) {
        return $thelist;

    // Add a message after a comma separated listing.
    if ( ',' == $separator ) {
        $thelist .= " (* not all assigned categories are being listed)";

    return $thelist;
add_filter( 'c2c_parentless_categories_list', 'customize_c2c_parentless_categories_list' );

c2c_get_parentless_categories_omit_ancestors (filter)

The ‘c2c_get_parentless_categories_omit_ancestors’ filter allows you to customize or override 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. By default, this argument is true.


  • bool $omit_ancestors : the $omit_categories argument sent to the function, otherwise implicitly assumed to be the default


// Don't omit ancestors unless they are the immediate parent of an assigned category
add_filter( 'c2c_get_parentless_categories_omit_ancestors', '__return_false' );


  1. Install via the built-in WordPress plugin installer. Or download and unzip 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_parentless_categories_list’ to filter parentless category listing
  4. Use the template tag <?php c2c_parentless_categories(); ?> in a theme template somewhere inside “the loop”


Installation Instructions
  1. Install via the built-in WordPress plugin installer. Or download and unzip 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_parentless_categories_list’ to filter parentless category listing
  4. Use the template tag <?php c2c_parentless_categories(); ?> in a theme template somewhere inside “the loop”
Why isn’t an assigned category for the post showing up in the `c2c_parentless_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.

Does this plugin include unit tests?


Contributors & Developers

“Parentless Categories” is open source software. The following people have contributed to this plugin.



2.0.5 (2018-05-20)

  • New: Add
  • New: Add GitHub link to readme
  • Change: Minor whitespace tweaks to unit test bootstrap
  • Change: Rename readme.txt section from ‘Filters’ to ‘Hooks’
  • Change: Modify formatting of hook name in readme to prevent being uppercased when shown in the Plugin Directory
  • Change: Tweaks installation instructions
  • Change: Note compatibility through WP 4.9+
  • Change: Update copyright date (2018)

2.0.4 (2017-02-13)

  • Fix: Replace use of deprecated (in WP 4.7) _usort_terms_by_name() with wp_list_sort() for WP 4.7+
  • Change: Update unit test bootstrap
    • Default WP_TESTS_DIR to /tmp/wordpress-tests-lib rather than erroring out if not defined via environment variable
    • Enable more error output for unit tests
  • Change: Note compatibility through WP 4.7+
  • Change: Minor readme.txt content and formatting tweaks
  • Change: Update copyright date (2017)
  • New: Add LICENSE file

2.0.3 (2016-02-02)

  • New: Define ‘Text Domain’ plugin header attribute.
  • New: Create empty index.php to prevent files from being listed if web server has enabled directory listings.
  • Change: Explicitly declare methods in unit tests as public.
  • Change: Minor reformatting and improvements to internal code documentation.
  • Change: Note compatibility through WP 4.4+.
  • Change: Update copyright date (2016).

2.0.2 (2015-02-11)

  • Note compatibility through WP 4.1+
  • Update copyright date (2015)

2.0.1 (2014-08-30)

  • Minor plugin header reformatting
  • Add check to prevent execution of code if file is directly accessed
  • Change documentation links to to be https
  • Note compatibility through WP 4.0+
  • Add plugin icon

2.0 (2014-01-09)

  • Add c2c_parentless_categories()
  • Deprecate parentless_categories() in favor of c2c_parentless_categories()
  • Change default behavior of c2c_parentless_categories() to omit all ancestor categories by default, instead of just directly assigned categories
  • Add optional arg $omit_ancestors to c2c_parentless_categories() only omitting direct parent categories and not all ancestor categories
  • Add filter ‘c2c_get_parentless_categories_omit_ancestors’
  • Add filter ‘c2c_parentless_categories’ to support filter invocation method c2c_parentless_categories()
  • Add c2c_get_parentless_categories_list()
  • Deprecate get_parentless_categories_list() in favor of c2c_get_parentless_categories_list()
  • Add filter ‘c2c_get_parentless_categories_list’ to support filter invocation method c2c_get_parentless_categories_list()
  • Add c2c_get_parentless_categories()
  • Deprecate get_parentless_categories() in favor of c2c_get_parentless_categories()
  • Add filter ‘c2c_get_parentless_categories’ to support filter invocation method c2c_get_parentless_categories()
  • Add filter ‘c2c_parentless_categories_list’ (which also passed $post_id to the hook)
  • Deprecate ‘parentless_categories’ in favor of ‘c2c_parentless_categories_list’
  • Remove harcoded space added after custom separator in c2c_get_parentless_categories_list()
  • Add unit tests
  • Add Filters section to readme.txt to document all filters
  • Note compatibility through WP 3.8+
  • Drop compatibility with versions of WP older than 3.6
  • Update copyright date (2014)
  • Code and documentation reformatting (spacing, bracing)
  • Change donate link
  • Add banner image


  • Change description in plugin header to make it shorter
  • Note compatibility through WP 3.5+
  • Update copyright date (2013)


  • Re-license as GPLv2 or later (from X11)
  • Add ‘License’ and ‘License URI’ header tags to readme.txt and plugin file
  • Remove ending PHP close tag
  • Note compatibility through WP 3.4+


  • Note compatibility through WP 3.3+
  • Add link to plugin directory page to readme.txt
  • Update copyright date (2012)


  • Note compatibility through WP 3.2+
  • Minor documentation reformatting in readme.txt
  • Fix plugin homepage and author links in description in readme.txt


  • Documentation tweaks
  • Note compatibility with WP 3.1+
  • Update copyright date (2011)


  • Wrap all functions in if(!function_exists()) check
  • Remove docs from top of plugin file (all that and more are in readme.txt)
  • Note compatibility with WP 2.9+, 3.0+
  • Add PHPDoc documentation
  • Minor tweaks to code formatting (spacing)
  • Add package info to top of plugin file
  • Add Changelog, Template Tags, and Upgrade Notice sections to readme.txt
  • Update copyright date
  • Remove trailing whitespace


  • Initial release