admin管理员组文章数量:1125596
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?
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?
Share Improve this question asked Feb 28, 2024 at 17:33 Andrea SacconiAndrea Sacconi 797 bronze badges 2 |1 Answer
Reset to default 2To 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
]);
本文标签: phpCreating an array with gettexed terms
版权声明:本文标题:php - Creating an array with gettexed terms 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736634919a1945856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
__
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 ♦ Commented Feb 28, 2024 at 19:01