WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Showing subcategories of a parent page under subcategory (31 posts)

  1. akis
    Member
    Posted 4 years ago #

    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.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  3. akis
    Member
    Posted 4 years ago #

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

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  5. akis
    Member
    Posted 4 years ago #

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

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    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; ?>
  7. akis
    Member
    Posted 4 years ago #

    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?

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    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
    }
    ?>
  9. akis
    Member
    Posted 4 years ago #

    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.

  10. akis
    Member
    Posted 4 years ago #

    edited.

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  12. akis
    Member
    Posted 4 years ago #

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

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  14. akis
    Member
    Posted 4 years ago #

    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.

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  16. Mark / t31os
    Moderator
    Posted 4 years ago #

    lol, i spoke to soon, i've broken it, hold on...

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

  17. akis
    Member
    Posted 4 years ago #

    It works! Thanks so much!

  18. Mark / t31os
    Moderator
    Posted 4 years ago #

    Tried the updated copy, i've edited the post 2 times since posting...;)

  19. akis
    Member
    Posted 4 years ago #

    It is possible to display the same menu on category child posts?

  20. Mark / t31os
    Moderator
    Posted 4 years ago #

    Do you mean display the menu when viewing a single post?

  21. akis
    Member
    Posted 4 years ago #

    Yes, but I already solved this.
    Thanks again for such a piece of code.

  22. Mark / t31os
    Moderator
    Posted 4 years ago #

    No problem... :)

  23. klarabella
    Member
    Posted 4 years ago #

    Hello everybody,
    I hope it's okay to just add my request for help since akis' post is nearly exactly what i am looking for. This piece of code t31os_ postet above nearly works for me, but I still could use two tweaks to it:

    Do you mean display the menu when viewing a single post?

    A pity for me akis already knew how to solve this. ;-) I need the menu to show the current child category and it's siblings when viewing a post.

    Plus: I need a rule for showing the child categorys of the standard category when on the home page. This part I might solve for myself but I'm afraid it wouldn't be very effective or elegant.

    If someone could add these features for me to the code snippet that would be appreciated very much.

  24. Mark / t31os
    Moderator
    Posted 4 years ago #

    Would wp_list_categories not be better suited to you?..

    http://codex.wordpress.org/Template_Tags/wp_list_categories

  25. klarabella
    Member
    Posted 4 years ago #

    Hm, I'm afraid I have to explain what I want to do and why. It's all about using WordPress more like a CMS and melting the navigation of pages and categories a bit. It's probably better to start a new topic for my problem anyway. But thank you again for this first feedback.

  26. Mark / t31os
    Moderator
    Posted 4 years ago #

    Ok, no problem, i'll keep a look out for your thread.

  27. akis
    Member
    Posted 4 years ago #

    I forgot about this thread so here it is my code, Klarabella.
    Maybe there is a simple way for this, but it's working.

    $category = get_the_category();
    $cat_term_id = $category[0]->term_id;
    $cat_category_parent = $category[0]->category_parent;
    $listcat = wp_list_categories('echo=0&child_of='.$cat_category_parent.'&title_li=');
    $listcat = str_replace("cat-item-".$cat_term_id, "cat-item-".$cat_term_id." current-cat", $listcat);
    
    if ( in_category( $cat_term_id ) || post_is_in_descendant_category( $cat_term_id )) {
    echo $listcat;
    }

    post_is_in_descendant_category function

    function post_is_in_descendant_category( $cats, $_post = null )
    {
    foreach ( (array) $cats as $cat ) {
    $descendants = get_term_children( (int) $cat, 'category');
    if ( $descendants && in_category( $descendants, $_post ) )
    return true;
    }
    return false;
    }
  28. kbuntu
    Member
    Posted 4 years ago #

    Hi t31os,

    Wonder if this thread is still active. I have the same problem as klarabella.
    I've used "akis" code but for whatever reason it show all the categories on some pages and sometimes it work as it supposed too.

    You code works great however is it possible to show all the subcategories of a parent a post is filed under instead showing all the parent categories?

    Thank you.

  29. steweill
    Member
    Posted 4 years ago #

    Thanks t31os_ for that idea above to grab the subcategories and echo them out of the foreach loop.

    I was wondering if there's an easy way to order this array by the date each one was created. I'm not loving the alphabetical (asc or desc) defaults.

    Just wondering...

    Thanks again!

  30. Magnificat
    Member
    Posted 4 years ago #

    I was pleased to find this thread still alive. I believe this is what I am looking for but I would appreciate some help in determining if that is the case.

    I need for the subcategories to remain visible (to function as navigation) when viewing a subcategory post. I would be very grateful if someone could clarify which of the codes in this thread would accomplish this and exactly where the code should be inserted.

    I am running 2.9. Thank you in advance!!

Topic Closed

This topic has been closed to new replies.

About this Topic