WordPress.org

Ready to get started?Download WordPress

Forums

3-level nav: need to drill back up - help? (7 posts)

  1. steweill
    Member
    Posted 4 years ago #

    Is there a simple way to get the parent category (if the page you're on - so to speak - is a sub-sub-category), and then populate an array with its children's children?

    Thanks in advance anyone :)

  2. steweill
    Member
    Posted 4 years ago #

    Actually I have changed things a bit. My new question - altho related - is this:

    How do you get the topmost parent category, then it's child's children?

  3. esmi
    Forum Moderator
    Posted 4 years ago #

    <?php $parent_cats = strip_tags( wp_list_categories('title_li&depth=1&echo=0') );?>

    will give you a list of all top level categories. You could then use cat_is_ancestor_of to determine which of these is your current sub-cat's ancestor. Once you have the right ancestor name, get_cat_ID will provide its ID which you can then use with get_categories to obtain an array of the whole tree or with wp_list_categories to display the tree.

    <?php
    $this_category = get_category($cat);
    $parent_cats = strip_tags( wp_list_categories('title_li&depth=1&echo=0') );
    foreach( $parent_cats as $parent_cat ) {
    	$parent_id = get_cat_ID( $parent_cat );
    	if(cat_is_ancestor_of( $parent_id, $this_category->cat_ID ) {
    		echo '<pre>';
    		print_r(get_categories('&child_of='. $parent_id) );
    		echo '</pre>';
    	}
    }
    ?>
  4. steweill
    Member
    Posted 4 years ago #

    Thanks so much esmi -

    I've been troubleshooting around with this code you left, and I seem to have trouble getting
    strip_tags( wp_list_categories('title_li&depth=1&echo=0') );

    to return an array, I'm getting a string. How can I get it to return (or convert to) an array? Does echo=0 render it a string? can we create an array with get_terms()?

    Thanks again.

  5. steweill
    Member
    Posted 4 years ago #

    Hi again esmi,

    I've used explode() and it appears I'm getting an array of one: all categories are being output. They all get output again if I just echo the array iteration $parentcat[0]. Nothing comes up for $parentcat[1] and so on. If I output $parent_cats variable it doe indeed output "Array". Do you know how I could break it down further so I can then go ahead and test it for 'cat_is_ancestor_of'.


    if(is_category())
    {
    $this_category = get_query_var('cat');
    $parent_cats = strip_tags( wp_list_categories('title_li=&depth=1&echo=0') );

    $parent_cats = explode(",", $parent_cats);

    foreach ($parent_cats as $parentcat) {
    echo "$parentcat";

    }

    }

    I'm not a php coder so some basic stuff eludes me - sorry.

  6. esmi
    Forum Moderator
    Posted 4 years ago #

    Sorry - I missed a step. Amended code:

    <?php
    $this_category = get_category($cat);
    $parents = strip_tags( wp_list_categories('title_li&depth=1&echo=0') );
    $parent_cats = explode("\n\n",$parents);
    foreach( $parent_cats as $parent_cat ) {
    	$parent_id = get_cat_ID( trim($parent_cat) );
    	if(cat_is_ancestor_of( $parent_id, $this_category->cat_ID ) ) {
    		echo '<pre>';
    		print_r(get_categories('&child_of='. $parent_id) );
    		echo '</pre>';
    	}
    }
    ?>
  7. PaulWarren
    Member
    Posted 3 years ago #

    This is so close to what I am looking for... how would I amend this code to get the topmost category's child categories rather then it's child's child's categories?

Topic Closed

This topic has been closed to new replies.

About this Topic