WordPress.org

Ready to get started?Download WordPress

Forums

Loop children of each parent category (3 posts)

  1. hiphopinenglish
    Member
    Posted 8 months ago #

    I'm calling all parents in a category, and I'd like to then call each of their children to do some stuff with. I've called each parent just fine, but now I need a foreach to call their children. I'm struggling with what to call. Here's what I have so far:

    <?php echo "<ul>";
    $args_list = array(
    		'taxonomy' => 'hhie_artists', // Registered tax name
    		'show_count' => false,
    		'hierarchical' => true,
    		'title_li' => '',
    		'echo' => '0',
    		'parent' => '0',
    		);	 
    
    $categories = get_categories($args_list);
    		foreach ($categories as $category) { ?>
    		<li><a href="<?php get_category_link($category); ?>"><?php echo $category->name ?></a></li>
    
    <?php $children = get_categories('child_of=' .$category); <<Here's my problem
    		foreach ($children as $child) {
    		// Do stuff with the children
    
    <?php } //endforeach children
    	} //endforeach category (parent)
    echo '</ul>';
    ?>

    Any help naturally greatly appreciated.

  2. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    try to use the category id;

    <?php $children = get_categories('child_of=' .$category->term_id);

    http://codex.wordpress.org/Function_Reference/get_categories

  3. hiphopinenglish
    Member
    Posted 8 months ago #

    Thanks for the suggestion alchymyth. I tried that a bit earlier, but now I'm returned a linked list of parent items, and nothing else.

    This is the full code I'm working with, including what I want to do inside my foreach:

    <?php
    								echo '<ul style="list-style-type: none;">';
    				$args_list = array(
    					'taxonomy' => 'hhie_artists', // Registered tax name
    					'show_count' => false,
    					'hierarchical' => true,
    					'title_li' => '',
    					'echo' => '0',
    					'parent' => '0',
    				);
    				$categories = get_categories($args_list);
    					foreach ($categories as $category) { ?>
    					<li><a href="<?php get_category_link($category); ?>"><?php echo $category->name ?></a></li>
    					<?php $children = get_categories('child_of=' . $category->term_id);
    					foreach ($children as $child) {
    					$array = get_option('taxonomy_' . $child->term_id);
    					$releasedate=$array['release_date_meta']; ?>
    					<li><a href="<?php get_category_link($child); ?>"><?php echo $child->name ?></a><?php if (!empty ($releasedate)): echo " - ". $releasedate; endif; ?></li>
    					<?php }
    					}
    
    				echo '</ul>';
    				?>

Reply

You must log in to post.

About this Topic