To adapt the second part of your code to work with the first part, where you are using wp_dropdown_categories
to generate the dropdown list, you can use the get_terms
function to retrieve all terms of the tipologia
taxonomy and then iterate over them to create your $names_trans
array.
In the wp_dropdown_categories
call, you pass the $names_trans array to a custom walker class Custom_Taxonomy_Dropdown_Walker
(you will need to define this walker class) which will use the translations for rendering the dropdown options.
// Retrieve all terms for 'tipologia' taxonomy
$tipologia_terms = get_terms(array(
'taxonomy' => 'tipologia',
'hide_empty' => false, // Include empty terms as well
));
// Initialize an empty array to store term translations
$names_trans = array();
// Iterate over each term and add its translation to the array
foreach ($tipologia_terms as $term) {
$names_trans[$term->term_id] = __($term->name, 'sacconicase');
}
// Now you have your $names_trans array with translations of tipologia terms
// Now in your wp_dropdown_categories call, you can use the $names_trans array to translate the terms
$taxonomy = wp_dropdown_categories([
'hierarchical' => false,
'name' => 'tipologia',
'taxonomy' => 'tipologia',
'selected' => $select,
'show_option_all' => esc_html__('Typology', 'sacconicase'),
'value_field' => 'slug',
'echo' => false,
'show_option_none' => __('Select a typology', 'sacconicase'), // Option to display if no terms are found
'option_none_value' => '', // Value to be sent if the "Select a typology" option is selected
'orderby' => 'name', // Order terms alphabetically by name
'order' => 'ASC', // Order in ascending order
'walker' => new Custom_Taxonomy_Dropdown_Walker($names_trans), // Pass $names_trans to custom walker
]);