Title: Category Navigation on single.php
Last modified: August 19, 2016

---

# Category Navigation on single.php

 *  [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/)
 * Hi!
 * I have a tricky question. I use a category-based navigation with subcategories
   and posts. I have the category navigation on top and a local navigation (on the
   left) shown below. what i need is that the working code for the category-page
   also works on the single.php or i use a different one with the same result:
 * **THATS WHAT I WANT** ( [http://www.goodandbad.net/sport-akustik/category/know-how/](http://www.goodandbad.net/sport-akustik/category/know-how/)):
   
   title category parent category – posts in category subcategory 1(if existion)–
   posts in subcategory subcategory 2(if existion) – posts in subcategory
 * **HERE IS THE CODE:**
 * <backticks>
 * <h2><?php echo $catname; ?></h2>
 *  <?php $categories = get_categories(“child_of=5”); foreach ($categories as $cat){?
   >
 *  <?php query_posts(“cat=$cat->cat_ID&showposts=-1&order=ASC&orderby=name”); ?
   >
    <h3><?php single_cat_title(); ?></h3> <?php while (have_posts()) : the_post();?
   >
 *  <div class=”what”>
    <h3>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute();?
   >”><?php the_title(); ?></h3> </div> <?php endwhile; ?> <?php } ?>
 * </backticks>

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/category-navigation-on-singlephp/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/category-navigation-on-singlephp/page/2/?output_format=md)

 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691350)
 * Un-tested but something like this, as the page is already filtered on a single
   category?
 *     ```
       <?php $currcategory = get_the_category($post->ID); ?>
       <?php $catid = $currcategory->cat_ID; ?>
       <?php $catchildren= 'child_of="'.$catid.'"'; ?>
       <?php $categories = get_categories($catchildren); foreach ($categories as $cat) { ?>
            <!-- Do Loop Stuff -->
       <?php } ?>
       <?php endif; ?>
       ```
   
 * HTH
 * David
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691549)
 * Thanks David for your code.
    Unfortunately the result is that nothing is displayed.
   the complete code in the sidebar is:
 *     ```
       foreach((get_the_category()) as $category)
       	{
           $postcat= $category->cat_ID;
           $catname =$category->cat_name;
           }
       <h2><?php echo $catname; ?></h2>
       	<?php $categories = get_categories("child_of=5"); foreach ($categories as $cat) { ?>
       	<?php query_posts("cat=$cat->cat_ID&showposts=-1&order=ASC&orderby=name"); ?>
       		<h3><?php single_cat_title(); ?></h3>
       		<?php while (have_posts()) : the_post(); ?>
   
       		<div class="what">
       			<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
       			<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
       		</div>
       	<?php endwhile; ?>
       <?php } ?>
       ```
   
 * to make myself clear:
    a code on the single.php which echos the subcategory and
   its posts would be great
 *  [Marvel Labs](https://wordpress.org/support/users/royalprince/)
 * (@royalprince)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691552)
 * Visit [http://www.z-oc.com/blog/zoc-subcats/](http://www.z-oc.com/blog/zoc-subcats/)…
 * You can modify it to suit your purpose
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691554)
 * [@royalprince](https://wordpress.org/support/users/royalprince/): i need the 
   subcategories and the posts within .. but thanks
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691574)
 * I was off base here, I thought it was the category in the top menu and the sub
   categories in the sidebar, like a sidebar sub menu.
 * But you want a sub category header and then the post titles in the sidebar?
 * I am not sure now of what you are after, is this just the page content or page
   content and sidebar you want?
 * EDIT: I get it now, I have some code that does this in the content area, I am
   ar work now will have a look later.
 * HTH
 * David
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691579)
 * Hi David. Thanks for your response. To make it clear look at my textpage :
 * [http://www.goodandbad.net/sport-akustik/category/know-how/](http://www.goodandbad.net/sport-akustik/category/know-how/)
 * On the left site you see the navigation structure
 * Title current category
    -title subcategory 1 posts within -title subcategory 
   2 posts within
 * and this is what i want to see on my single.php
 * thanks
 *  [Chinmoy](https://wordpress.org/support/users/chinmoy/)
 * (@chinmoy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691582)
 *     ```
       $postcat= $category->cat_ID;
       $catname =$category->cat_name;
       ```
   
 * would be
 *     ```
       $postcat= $category->term_id;
       $catname =$category->name;
       ```
   
 *  [Chinmoy](https://wordpress.org/support/users/chinmoy/)
 * (@chinmoy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691584)
 *     ```
       <?php
       $post_categories = wp_get_post_categories( $post->ID );
       foreach($post_categories as $c){
         $cat = get_category( $c );
         echo "<h1>". $cat->name . "</h1>";
         $childCats = get_categories( array('child_of' => $c) );
         if(is_array($childCats)):
           foreach($childCats as $child){ ?>
               <h2><?php echo $child->name; ?></h2>
       <?php
               query('cat='.$child->term_id);
               while(have_posts()): the_post(); $do_not_duplicate = $post->ID;
                //post code stuff here;
               endwhile;
               wp_reset_query();
           }
         endif;
       }
       ?>
       ```
   
 * write this on your single.php file
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691585)
 * thanks chinmoy!
 * the result looks good. unfortunately the posts on the single.php are not going
   anywhere. it always shows the last post but i can’t go to the e.g. first post
   of the subcategory..
 * look at:
    [http://www.goodandbad.net/sport-akustik/limited/referenz/](http://www.goodandbad.net/sport-akustik/limited/referenz/)
 * THX
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691586)
 * thanks chinmoy again!
 * the fatal error msg:
    Fatal error: Call to undefined function: query() in /homepages/
   4/d188254744/htdocs/sport-akustik/wp-content/themes/Q/left-1.php on line 22
 * thx
 *  [Chinmoy](https://wordpress.org/support/users/chinmoy/)
 * (@chinmoy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691587)
 * sorry!! change `query('cat='.$child->term_id);` to
    `query_posts('cat='.$child-
   >term_id);`
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691589)
 * PERFECT!!!! thank you so much!
 * I would be glad if I could also understand it;-)
 *  [Chinmoy](https://wordpress.org/support/users/chinmoy/)
 * (@chinmoy)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691590)
 * you are welcome.:)
 * IF solve, plz mark your 2 threads as **RESOLVED.**
 *  Thread Starter [deftru](https://wordpress.org/support/users/deftru/)
 * (@deftru)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691591)
 * THIS IS THE FUNCTIONING CODE FROM chinmoy29:
 *     ```
       <?php
       $post_categories = wp_get_post_categories( $post->ID );
       foreach($post_categories as $c){
         $cat = get_category( $c );
         echo "<h1>". $cat->name . "</h1>";
         $childCats = get_categories( array('child_of' => $c) );
         if(is_array($childCats)):
           foreach($childCats as $child){ ?>
               <h2><?php echo $child->name; ?></h2>
       <?php
               query_posts('cat='.$child->term_id);
               while(have_posts()): the_post(); $do_not_duplicate = $post->ID;
                //post code stuff here;
               endwhile;
               wp_reset_query();
           }
         endif;
       }
       ?>
       ```
   
 * THX
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/#post-1691613)
 * Glad you have it sorted,
    I just looked and when you click on the link you loose
   the side menu structure top down, as the post id and post categories change.
 * Don’t you really need to check if there is any parents using the [get category parents](http://codex.wordpress.org/Function_Reference/get_category_parents),
   return nice name and get categories by slug, and then roll down from the top 
   level?
 * We just had a three hour local power cut so my evening is shot!
 * David

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/category-navigation-on-singlephp/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/category-navigation-on-singlephp/page/2/?output_format=md)

The topic ‘Category Navigation on single.php’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [navigation](https://wordpress.org/support/topic-tag/navigation/)
 * [single.php](https://wordpress.org/support/topic-tag/single-php/)
 * [subcategory](https://wordpress.org/support/topic-tag/subcategory/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 4 participants
 * Last reply from: [deftru](https://wordpress.org/support/users/deftru/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/category-navigation-on-singlephp/page/2/#post-1691637)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
