Select menu should display categories and the item’s depth

I have a select menu of a custom post type in the header.php:

    <?php 
    $selectloop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => -1 ) );
    echo '<form><select onchange="if (this.value) window.location.href=this.value">';
    while ( $selectloop->have_posts() ) : $selectloop->the_post(); ?>             
<option value="<?php the_permalink(); ?>" > <?php echo the_title(); ?></option> <?php
endwhile; 
echo '</select></form>';
wp_reset_query();
}
?>

It works fine, but since there are several categories with 3 or 4 levels, I’d like to visualize this by indent (or a minus or whatever).

For this to work I would also need the categories to show in the menu.
I looked into some code to find the item’s depth to start with but I can’t even get the ID of the current item in the loop 🙁

I’m aware I could use a nav walker for this task but I don’t think it’s necessary. Also, like this I’ll never have to touch the code again since it auto-updates if new items get added.

I’ll also gladly use any advise on how to improve the code 🙂

0

Leave a Comment