plugin development – How to show only the last two categories in a menu?

Question

I am working with many categories and their sub-categories (sub items). I felt the need for a customization, but I could not achieve exactly what I wanted.
Normally, categories and sub-categories are listed one after the other in the menu. To avoid confusion, I managed to separate them with the > sign and write them side by side. I used the Walker_CategoryDropdown class for this. For example, the following hierarchy;

A
  B
   C
   D
   E
    F
    G

Here is the menu (Select Option):

A
   A > B
    B > C
    B > D
    B > E
      E > F
      E > G

However, I want only the last 2 categories to appear all the time. According to the example above, it should look like this in the menu

   B > C
   B > D
   E > F
   E > G

Despite my research, I couldn’t find how to do this. In general, the examples are meant to show only the Main Categories.

<?php
//...
class New_Category_Dropdown extends Walker_CategoryDropdown {
        public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
            $pad = str_repeat('&nbsp;', $depth * 3);

            /** This filter is documented in wp-includes/category-template.php */
            $cat_name = apply_filters( 'list_cats', $category->name, $category );
       
            if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
                $value_field = $args['value_field'];
            } else {
                $value_field = 'term_id';
            }
     
            $output .= "t<option class="level-$depth" value="" . esc_attr( $category->{$value_field} ) . """;
            // Type-juggling causes false matches, so we force everything to a string.
            if ( (string) $category->{$value_field} === (string) $args['selected'] )
                $output .= ' selected="selected"';
            $output .= ' data-uri="'.get_term_link($category).'" '; 
            
            $output .= '>';
            $dt="";
            if($category->parent>0)
            {
                $cp = $category->parent;
                $trd = get_term_by('id', $cp, 'product_cat');
                $dt = $trd->name .' > ';
            }
            $output .= $pad.$dt.$cat_name;
            if ( $args['show_count'] )
                $output .= '&nbsp;&nbsp;('. number_format_i18n( $category->count ) .')';
            $output .= "</option>n";
       
        }
    }
//...
        $category_args =  array(
            'show_option_none' => __( '- Select a category -'),
            'hierarchical'     => 1,
            'hide_empty'       => 0,
            'name'             => 'product_cat',
            'id'               => 'product_cat',
            'taxonomy'         => 'product_cat',
            'orderby'          => 'name',
            'title_li'         => '',
            'class'            => 'product_cat new_-select2',
            'exclude'          => '',
            'selected'         => $selected_cat,
            'walker'           => new New_Category_Dropdown(),
        );
//...

0
ddisdevelpgelis 1 month 2021-12-14T16:28:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse