• Resolved akis

    (@akis)


    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 - 16 through 30 (of 30 total)
  • Thread Starter akis

    (@akis)

    It works! Thanks so much!

    Tried the updated copy, i’ve edited the post 2 times since posting…;)

    Thread Starter akis

    (@akis)

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

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

    Thread Starter akis

    (@akis)

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

    No problem… ??

    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.

    Would wp_list_categories not be better suited to you?..

    https://codex.www.ads-software.com/Template_Tags/wp_list_categories

    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.

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

    Thread Starter akis

    (@akis)

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

    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.

    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!

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

    Hello t31os_

    “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…”

    I tried trimming it down and I can’t get it to work. I only want to show the sub-categories in the body area if there are any. I have the parent categories listed already in the header.

    Here is what I’m using…

    <?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);
    }
    ?>

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