WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Showing subcategories of a parent page under subcategory

[Resolved] Showing subcategories of a parent page under subcategory

  • Sound creepy my formulation but here are the details:

    VIEWING: CAT1
    SHOWING: SUBCAT1, SUBCAT2, SUBCAT3

    VIEWING: SUBCAT1 / SUBCAT2 / SUBCAT3
    SHOWING: NO CATEGORIES

    what I want:

    VIEWING: SUBCAT1 / SUBCAT2 / SUBCAT3
    SHOWING: SUBCAT1, SUBCAT2, SUBCAT3

    What I’ve had tried:

    $cat = get_query_var('cat');
    $cats = wp_list_categories('&child_of='.$cat);

    But doesn’t seem to work.

Viewing 15 replies - 1 through 15 (of 30 total)
  • Remove the & from the wp_list_categories, if there’s nothing else infront of it, then it shouldn’t be there.

    Also if you want to store the value from wp_list_categories in a variable then you need to use echo=0…

    $cats = wp_list_categories('echo=0&child_of='.$cat);

    or

    $cats = wp_list_categories('child_of='.$cat.'&echo=0');

    Both will do the same.

    Yes, that was badly copy/paste.
    Thanks you for your reply but this doesn’t solve the real problem.

    Well i’ll need to see more of your code to have any idea of what you’re trying to do with the results…

    Right now you’re storing the data from the wp_list_categories in a variable..

    If that data is not echo’ed or printed then you’ll get nothing appear on the screen.

    I can’t help if you don’t give me either…

    1. More details of what you’re trying to accomplish.
    2. Information about what the output is, if it’s not what you’re expecting it to be.

    The more details and code you can provide the better.

    I’ve answered at least one thread relating to this function already today, and i’m confident the function operates as it should when used correctly…

    <?php
    $cat = get_query_var('cat');
    $cats = wp_list_categories('title_li=&echo=0&orderby=id&use_desc_for_title=1&hide_empty=0&child_of='.$cat);
    
    if(is_category()) :
    ?>
    
    <ul>
    <?php echo $cats; ?>
    </ul>
    
    <?php endif; ?>

    This is the complete code and it’s working fine on parent category but I want to display the SAME (with parent childs) menu on child category.

    The problem is get_query_var(‘cat’) because this output current category ID and it’s not exactly what I want.

    Thanks for your help,
    Akis

    Try this..

    <?php
    if(is_category()) :
    $cat = get_query_var('cat');
    $termcat = get_term($cat,'category');
    
    if($termcat->parent == 0) {
    	// If the category doesn't have a parent
    	$cats = wp_list_categories('title_li=&echo=0&orderby=ID&use_desc_for_title=1&hide_empty=0&depth=1');
    }
    else {
    	// Else it does have a parent
    	$cats = wp_list_categories('title_li=&echo=0&orderby=ID&use_desc_for_title=1&hide_empty=0&child_of='.$cat);
    }
    unset($termcat);
    ?>
    <ul>
    <?php echo $cats; ?>
    </ul>
    <?php endif; ?>

    Thanks but doesn’t seem to work and looks preety complicated.

    ./category/wordpress/
    show menu (ul.li) with wordpress category childs

    ./category/wordpress/themes/
    show menu (ul.li) with wordpress category childs (the same)

    ./category/wordpress/plugins/
    show menu (ul.li) with wordpress category childs (the same)

    and so on.
    It’s that complicated?

    Here try this..

    <?php
    if(is_category()) {
    	$subcategories = get_terms('category', 'parent='.get_query_var('cat'));
    
    	if(empty($subcategories)) {
    		$thiscat = get_term(get_query_var('cat'),'category');
    		$subcategories = get_terms('category', 'parent='.$thiscat->parent.'');
    	}
    	if(empty($subcategories)) $subcategories = array();
    	if(!empty($subcategories)) {
    		echo '<ul>';
    
    		foreach($subcategories as $subcat) {
    			if(get_query_var('cat') == $subcat->term_id) $current = ' current-cat'; else $current = '';
    			echo '
    			<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>';
    	}
    }
    else {
    	// If no current cat query, just get the top level ones using wp_list_categories.
    	?>
    	<ul>
    		<?php wp_list_categories('title_li=&depth=1');?>
    	</ul>
    	<?php
    }
    ?>

    I’m using just the first part, before else and it works fine if category has 1+ childs WITH posts.
    If not it displays all parent categories and it’s not exactly what I want – just hide them.

    Thanks so much for your help.

    edited.

    No problem if you can take anything away from it then i’m happy to have helped.

    I’m actually using this version on my test install at the moment.

    <?php
    if(is_category()) {
    
    	$breakpoint = 0;
    	$subcategories = get_terms('category', 'parent='.get_query_var('cat'));
    	$thiscat = get_term(get_query_var('cat'),'category');
    
    	while(empty($subcategories)) {
    		$subcategories = get_terms('category', 'parent='.$thiscat->parent.'');
    		$breakpoint++;
    		if($breakpoint > 10) break; // Avoid infinite loop (you never know)
    	}
    
    	$items='';
    	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.' ('.$subcat->count.' posts)</a>
    		</li>';
    	}
    	echo "<ul>$items</ul>";
    }
    ?>

    Shows sub-cats if there are any, else show same level (sibling) cats. I thought the above a little nicer to the previous code…

    Thanks, but, how can I stop parent categories from being displayed if current category has no childs or has no posts under child?

    Maybe..

    <?php
    if(is_category()) {
    
    	$breakpoint = 0;
    	$subcategories = get_terms('category', 'parent='.get_query_var('cat'));
    	$thiscat = get_term(get_query_var('cat'),'category');
    
    	while(empty($subcategories)) {
    		$subcategories = get_terms('category', 'parent='.$thiscat->parent.'');
    		$breakpoint++;
    		if($breakpoint > 10) break; // Avoid infinite loop (you never know)
    	}
    
    	$items='';
    	if($thiscat->parent != 0 || $breakpoint == 0) {
    		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.' ('.$subcat->count.' posts)</a>
    			</li>';
    		}
    	}
    	echo "<ul>$items</ul>";
    }
    ?>

    You can of course remove what you don’t need..

    If you only ever want to display sub-categories of the current category (and only when they exist) i can actually trim the above down somewhat, you don’t need the while loop if you’re never showning top level or parent categories…

    Many thanks, now it works as it should.
    I have tried to remove the while but doesn’t seem to work correctly without it.

    Sorry for by bad english.

    Ok then, i think for your particular usage the empty checks were better..

    This should do what you want..

    <?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.' ('.$subcat->count.' posts)</a>
    			</li>';
    		}
    		echo "<ul>$items</ul>";
    	}
    	unset($subcategories,$subcat,$thiscat,$items);
    }
    ?>

    EDIT: Updated, should work now.

    lol, i spoke to soon, i’ve broken it, hold on…

    EDIT: Updated the above post, should be good to go now.

Viewing 15 replies - 1 through 15 (of 30 total)
  • The topic ‘[Resolved] Showing subcategories of a parent page under subcategory’ is closed to new replies.