0

In my search-box function I have this piece

$select = $wp_query->get('tipologia');
$select = '' == $select ? 0 : $select;
$taxonomy = wp_dropdown_categories([
    'hierarchical' => false,
    'name' => 'tipologia',
    'taxonomy' => 'tipologia',
    'selected' => $select,
    'show_option_all' => esc_html__('Typology', 'sacconicase'),
    'value_field' => 'slug',
    'echo' => false
]);

I’d like to add an array to my ‘taxonomy’ => ‘tipologia’, so that I can gettext all the taxonomy terms like here:

$names_trans = array(

350 => __('Apartment', 'sacconicase'),

354 => __('Apartment in villa', 'sacconicase'),


$terms = $names_trans[ get_the_terms( $post->ID, 'tipologia')[0]->term_id ];

How could I adapt the second part of the code to the first?

2
  • __ and the internationalisation API is meant for static hardcoded strings, it's not meant to be used with terms and other dynamic data from the database! Doing this could lead to all sorts of unintended problems and security issues
    – Tom J Nowell
    Feb 28 at 19:01
  • @TomJNowell I used this solution only with this taxonomy because the terms of this taxonomy are actually used as static, that is, once I define the typology of the apartments, these terms are always the same: "apartment", "villa" and so on, I dont have to add or change terms frequently. But if you say it's dangerous... Feb 28 at 19:12

1 Answer 1

1

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
]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.