• 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: https://doubleagents.webfactional.com/site/
    Content page: https://doubleagents.webfactional.com/site/archives/171

    Many thanks in advance,
    Andrew

Viewing 9 replies - 1 through 9 (of 9 total)
  • What determines which categories are shown on the single pages?

    Thread Starter andrewfox

    (@andrewfox)

    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: https://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

    (@andrewfox)

    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

    (@andrewfox)

    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

    (@andrewfox)

    Run into problem: the categories are not in nested order.
    Problem here: https://doubleagents.webfactional.com/site/archives/143

    Any ideas?

    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

    (@andrewfox)

    Hi,

    Yeah the problem I have now is that sub-categories aren’t grouped by parent categories.

    For example, on this page’s sidebar 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

    (@andrewfox)

    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 -->

    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.