A
A
Anton Litvinenko2020-08-05 21:11:47
WordPress
Anton Litvinenko, 2020-08-05 21:11:47

How to correctly put the path in wp_dropdown_categories?

Actually, I have already rummaged through everything I could, as far as my Google allowed me.
There is a taxonomy for the post type that determines the location of a person.
In the admin panel, these are areas and children of areas - settlements.
It looks something like this:

picture

5f2af35dc89e8157328805.png

I output the select using the following code:
The code

if ( is_tax() ) {
  $queried_object = get_queried_object();
  $this_category_id = $queried_object->term_id;
  $selected = $queried_object->slug;
} else {
  $this_category_id = '';
  $selected = '';
}

//echo $selected;

$args = array(
  'show_option_all'    => esc_html__( 'Всі регiони або населенні пункти', 'youth-worker' ),
  //'show_option_none' => __( 'Всі регiони', 'youth-worker' ),
  'option_none_value' => '',
  'orderby'      => 'name',
  'show_count'         => 1,
  'echo'               => 0,
  'hide_empty'         => 0,
  'hide_if_empty'      => 1,
  'hierarchical'       => 1,
  'value_field'        => 'slug',
  'depth'              => 1,
  'child_of'           => $this_category_id,
  'name'               => 'volunteers-location',
  'class'              => 'volunteers-location',
  'taxonomy'           => 'volunteers-location',
  //'selected'           => $selected,
); 

$dropdowncats  = wp_dropdown_categories( $args );
$dropdown_replace = "<select$1 onchange='return this.form.submit()'>"; 
$dropdowncats  = preg_replace( '#<select([^>]*)>#', $dropdown_replace, $dropdowncats ); 

?>


<?php if ( $dropdowncats ) : ?>

  <form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">

    <div class="search-category-wrapper">
      <div class="select-wrapper">
        <?php echo $dropdowncats; ?>
      </div>
    </div>

    <noscript>
      <input type="submit" value="<?php _e('Шукати', 'youth-worker') ?>">
    </noscript>

  </form>

<?php endif; ?>


since it is displayed not only on the taxonomy page, a check is made first.
I choose, for example, the Volyn region and the page of the taxonomy archive opens for me, in which the same code, I pull it from the template. Only now in the select city of Volyn region. Clicking on the city goes to the page of the city and the select is hidden.

urls like this:
youth-worker.loc/volunteers-location/vynnyczkaya-oblast

And everything seems to be working, but something doesn't work. If you select All regions in the select, then it throws a url like this:
youth-worker.loc/?volunteers-location=0

if you remove the line 'value_field' => 'slug' in the arguments, then all transitions go to 404 and urls like this :
youth-worker.loc/?volunteers-location=58

I also can’t set up the selected argument option, because it seems to come from the parent, if I remove 'child_of', then selected starts working, but naturally the hierarchy stops working as it should, showing only the main categories.

I'm missing something, but my eyes are already blurred.
I probably need to pass something to 'option_none_value' for the correct return to the beginning of all beginnings, but I don't understand what.
With selected, it also turns out that you need to somehow pass the current slug, but it also appears after the formation of the drop-down list

. I have already looked at the options with get_terms and get_categories, but so far the current option is best besides these little things.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question