Forums

show subcategories in sidebar when viewing single posts (14 posts)

  1. raune
    Member
    Posted 1 year ago #

    hi,

    i'm trying to use the categories as a navigation. So far i got the subcategories showing nicely when on a parent or sibbling in the sidebar using this code:

    <?php
    if(is_category()) {
    
    	$breakpoint = 0;
    	$thiscat = get_term( get_query_var('cat') , 'category' );
    	$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
    
    	if(empty($subcategories) && $thiscat->parent != 0) {
    		$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
    	}
    
    	$items='';
    	if(!empty($subcategories)) {
    		foreach($subcategories as $subcat) {
    			if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
    			$items .= '
    			<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
    				<a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.'</a>
    			</li>';
    		}
    		echo "<ul>$items</ul>";
    	}
    	unset($subcategories,$subcat,$thiscat,$items);
    }
    ?>

    which I got from this thread: http://wordpress.org/support/topic/showing-subcategories-of-a-parent-page-under-subcategory

    big thanks to t31os_ !

    my problem now is that the subcategories still need to show when viewing the individual posts. tried the code from akis but it doesn't work for me.

    quite new to this so pl keep answers simple, thanks!

  2. MAS
    Member
    Posted 1 year ago #

    use this

    <?php
    
    	$breakpoint = 0;
    	$thiscat = get_term( get_query_var('cat') , 'category' );
    	$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
    
    	if(empty($subcategories) && $thiscat->parent != 0) {
    		$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
    	}
    
    	$items='';
    	if(!empty($subcategories)) {
    		foreach($subcategories as $subcat) {
    			if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
    			$items .= '
    			<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
    				<a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'">'.$subcat->name.'</a>
    			</li>';
    		}
    		echo "<ul>$items</ul>";
    	}
    	unset($subcategories,$subcat,$thiscat,$items);
    
    ?>
  3. MAS
    Member
    Posted 1 year ago #

    or uses if(is_category() || is_single()) instead of if(is_category())

  4. raune
    Member
    Posted 1 year ago #

    ok that works but also shows the categories, i only want the subs when on the posts. is there a way to fix that?

  5. MAS
    Member
    Posted 1 year ago #

    r you use this

    uses if(is_category() || is_single()) instead of if(is_category())

  6. raune
    Member
    Posted 1 year ago #

    sorry i don't get it, can't find where it says if(is_category())

  7. MAS
    Member
    Posted 1 year ago #

    see your post...there it is.

  8. raune
    Member
    Posted 1 year ago #

    ah ok , was looking at your code..

  9. raune
    Member
    Posted 1 year ago #

    still no luck, categories showing when viewing the post

  10. MAS
    Member
    Posted 1 year ago #

    r you want all category and subcategory of your site or post specific category and sub category

  11. raune
    Member
    Posted 1 year ago #

    only post specific subcategory

  12. MAS
    Member
    Posted 1 year ago #

    so this function will not work..need some modification

  13. raune
    Member
    Posted 1 year ago #

    and the siblings of that subcategory.

    I got the categories in my main nav, then when selected the specific subcategories show in the sidebar, and I want them to stay that way when viewing the posts belonging to any of those subcategories

  14. MAS
    Member
    Posted 1 year ago #

    it is possible but need some modification of that function.

Topic Closed

This topic has been closed to new replies.

About this Topic