Title: Nested categories for single.php
Last modified: August 19, 2016

---

# Nested categories for single.php

 *  [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/)
 * Hi,
 * On a development version of a website I have a set of nested categories. On the
   homepage and archive pages we have a view of the categories that you can click
   on similar to below:
 * Category 1
    – Sub-category 1.1 – Sub-category 1.2 – Sub-category 1.3 Category
   2 – Sub-category 2.1 – Sub-category 2.2 Category 3 – Sub-category 3.1 – Sub-category
   3.2 – Sub-category 3.3 – Sub-category 3.4 etc…
 * I want on the single content page (single.php) to just show the chosen categories,
   but in a similar format, like the below:
 * Category 1
    – Sub-category 1.1 – Sub-category 1.3 Category 2 – None Category 
   3 – Sub-category 3.4
 * Is this possible? I’m amazed I’m unable to find something like this somewhere
   on the web.
 * The development website is here:
    Home: [http://doubleagents.webfactional.com/site/](http://doubleagents.webfactional.com/site/)
   Content page: [http://doubleagents.webfactional.com/site/archives/171](http://doubleagents.webfactional.com/site/archives/171)
 * Many thanks in advance,
    Andrew

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Peter Butler](https://wordpress.org/support/users/peterebutler/)
 * (@peterebutler)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1035811)
 * What determines which categories are shown on the single pages?
 *  Thread Starter [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1035818)
 * Thanks for getting back to me.
 * The categories are of the content itself (rather than all the categories in the
   website). For example, the categories of this content: [http://doubleagents.webfactional.com/site/archives/171](http://doubleagents.webfactional.com/site/archives/171)
   is Camden Arts Centre, Event, etc. Essentially the same as the_category(), but
   I can’t get this to organise itself well.
 * Does that make sense?
 * Thanks,
    Andrew
 *  Thread Starter [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1035987)
 * Think I have almost worked it all out… now just need to get that link working
   properly. I will post code later…
 *  Thread Starter [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1035988)
 * This is the code I’m using, to show a post’s set of nested sub-categories in 
   a definition list. It seems to be working, but it could probably be done more
   efficiently.
 * Notes:
    1. the parent categories aren’t links, but could easily be made to be
   2. it wouldn’t work well for sub-sub-categories (not an issue for this website)
 *     ```
       <dl id="categories">
       		<?php
       		foreach((get_the_category()) as $cat) {
       			if ($cat->category_parent == 0) {
       				// root/parent category (don't do anything - this should not have been a link - but it could be if that's the way you wanted it set-up)
       			} else {
       				// show category link
       				// get category_parent details (nicename and cat_name required)
       				// only show parent category if it is first one
       				if ($cat->category_parent != $oldCatParentId) {
       					$catParentName = get_cat_name( $cat->category_parent );
       					echo '<dt>'.$catParentName.':</dt>';
       				}
       				$oldCatParentId = $cat->category_parent;
       				echo '<dd><a href="'.get_category_link($cat->cat_ID).'">'. $cat->cat_name . '</a>' . '</dd>';
       			}
       		} ?>
       </dl> <!-- /#categories -->
       ```
   
 *  Thread Starter [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1035994)
 * Run into problem: the categories are not in nested order.
    Problem here: [http://doubleagents.webfactional.com/site/archives/143](http://doubleagents.webfactional.com/site/archives/143)
 * Any ideas?
 *  [Peter Butler](https://wordpress.org/support/users/peterebutler/)
 * (@peterebutler)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1036033)
 * Took me a bit to figure out what you’ve got going on there –
 * So, are you trying to display all child categories of a parent category together(
   So you dont get multiple lists of contributors, as you have on the page you showed)?
 *  Thread Starter [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1036065)
 * Hi,
 * Yeah the problem I have now is that sub-categories aren’t grouped by parent categories.
 * For example, on [this page’s sidebar](http://doubleagents.webfactional.com/site/archives/143)
   the parent category ‘Contributors’ is split into 6 sections. I need to do a php
   usort (?) on the object retrieved from get_the_category() so it can be ordered
   by category_parent. But my php skills lack in this area.
 * Any ideas?
 *  Thread Starter [andrewfox](https://wordpress.org/support/users/andrewfox/)
 * (@andrewfox)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1036177)
 * Okay – this is a way of doing it (if anyone else is interested)…
 *     ```
       <dl id="categories">
       <?php
       $organisedCats = get_the_category();
       $catOutput = "";
       $catArray = array();
   
       foreach((get_the_category()) as $cat) {
       	if ($cat->category_parent == 0) {
       		// root/parent category (don't do anything - this should not have been chosen)
       	} else {
       		if ($catArray[$cat->category_parent]) {
       			// if the array bit already exists, then dobn't do anything
       		} else {
       			// write the title of the category
       			$catParentName = get_cat_name( $cat->category_parent );
       			$catArray[$cat->category_parent] = '<dt>'.$catParentName.':</dt>';
       		}
       		// write in the specific category
       		$catArray[$cat->category_parent] .= '<dd><a href="'.get_category_link($cat->cat_ID).'">'. $cat->cat_name . '</a>' . '</dd>';
       	}
       }
       foreach($catArray as $cat) {
       	echo $cat;
       }
       ?>
   
       </dl> <!-- /#categories -->
       ```
   
 *  [r-a-y](https://wordpress.org/support/users/r-a-y/)
 * (@r-a-y)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1036206)
 * This is a great post!
    I need this to work with sub-sub-categories as well though.
 * Any idea how?

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Nested categories for single.php’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [category](https://wordpress.org/support/topic-tag/category/)
 * [get_the_category](https://wordpress.org/support/topic-tag/get_the_category/)
 * [nested](https://wordpress.org/support/topic-tag/nested/)
 * [Sub-Category](https://wordpress.org/support/topic-tag/sub-category/)

 * 9 replies
 * 3 participants
 * Last reply from: [r-a-y](https://wordpress.org/support/users/r-a-y/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/nested-categories-for-singlephp/#post-1036206)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
