Skip to content

Block to show sub-catagories from selected parent-catagorey. #10

Open

Description

@Nightingale13

As the title suggests, I am looking for a solution to show nested "sub-categories" from a selected "parent-category". Basically what the default "categories" block does but from a specific "parent-category". I'm using your plugin now and I think the logic is there, just needs a few tweaks.

I know Python quite well but I don't know PHP or javascript so I'm at a loss here. I'd be happy to help test it out in my site if that is of use.

Thank you!

Activity

  1. JagUlysse commented on Mar 14, 2025

    @JagUlysse

    That's exactly what I was looking for when I discovered the plugin!
    It works wonderfully well and I'm grateful for the work done!

    Categories are great but when you have lots of them, it creates a huge list with elements that are not always relevant. Being able to refine the list for subcategories would be amazing. Typically, I'd like to propose sub-elements from 'Clothes' or from 'Furnitures' but not all types at once.

    Looking forward to hear from you guys!

    EDIT:
    This can be hardcoded in 'build/taxonomy/render.php' by adding a 'child_of' but an option in the backend would be lovely.
    $terms = get_terms( [ 'hide_empty' => true, 'taxonomy' => $attributes['taxonomy'], 'number' => 100, 'child_of' => 162, //insert your parent category ID here ] );

  2. pigsound commented on Jun 10, 2025

    @pigsound

    oh cool,
    that's what i am looking for! i adapted the original code starting with your approach and customised it for my needs, where i make the dropdown list both parent and child categories, like this:
    parent 1
    –– child 1a
    –– child 1b
    parent 2
    –– child 2a
    –– child 2b
    –– child 2c
    it would be great to see this feature implemented in the plugin update some time in the future. in the mean time i'll have to hard code it in 'build/taxonomy/render.php'. i am sure my code can be improved – feel free to add your suggestions!

    code (starting from line 21):

    $terms = get_terms( [
    	'taxonomy'     => $attributes['taxonomy'],
    	'hide_empty'   => true,
    	'orderby'      => 'name',
    	'order'        => 'ASC',
    	'hierarchical' => true,
    	'number'       => 1000,
    ] );
    
    // Helper function: recursively generates the option tags
    function render_term_options_recursive( $terms, $base_url, $query_var, $page_var, $selected_slug = '', $parent = 0, $depth = 0 ) {
    	$output = '';
    
    	foreach ( $terms as $term ) {
    		if ( $term->parent == $parent ) {
    			$indent = str_repeat( '— ', $depth );
    			$url = esc_attr( add_query_arg( [ $query_var => $term->slug, $page_var => false ], $base_url ) );
    			$selected = selected( $term->slug, $selected_slug, false );
    
    			$output .= sprintf(
    				'<option value="%s"%s>%s%s</option>',
    				$url,
    				$selected,
    				$indent,
    				esc_html( $term->name )
    			);
    
    			$output .= render_term_options_recursive( $terms, $base_url, $query_var, $page_var, $selected_slug, $term->term_id, $depth + 1 );
    		}
    	}
    
    	return $output;
    }
    
    $selected_slug = wp_unslash( $_GET[ $query_var ] ?? '' );
    ?>
    
    <div <?php echo get_block_wrapper_attributes( [ 'class' => 'wp-block-query-filter' ] ); ?> data-wp-interactive="query-filter" data-wp-context="{}">
    	<label class="wp-block-query-filter-post-type__label wp-block-query-filter__label<?php echo $attributes['showLabel'] ? '' : ' screen-reader-text' ?>" for="<?php echo esc_attr( $id ); ?>">
    		<?php echo esc_html( $attributes['label'] ?? $taxonomy->label ); ?>
    	</label>
    	<select class="wp-block-query-filter-post-type__select wp-block-query-filter__select" id="<?php echo esc_attr( $id ); ?>" data-wp-on--change="actions.navigate">
    		<option value="<?php echo esc_attr( $base_url ); ?>"><?php echo esc_html( $attributes['emptyLabel'] ?: __( 'All', 'query-filter' ) ); ?></option>
    		<?php echo render_term_options_recursive( $terms, $base_url, $query_var, $page_var, $selected_slug ); ?>
    	</select>
    </div>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions