• sacconi

    (@sacconi)


    I’m approaching again a work that has left unfinished. I currently have the right hook to filter the title of the archive pages, what I need now is doing something similar for all the category terms, using the same custom meta fields. This is my current code: https://pastebin.com/Mk7qAg3r

    The purpose is , i.e., seeing the german name “Friaul Julisch Venetien” not only as archive page title, but also in the search box, when I’m on a german locale

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    I want to be sure I understand what you want. You want all the location terms in the first search dropdown field to read in German or English when those locales are in force? So the “Friuli Venezia-Giulia (117)” dropdown item should read “Friaul Julisch Venetien (117)” when a German locale is in force?

    The code you linked to isn’t very useful. What code do you use to generate the search box dropdown field?

    Thread Starter sacconi

    (@sacconi)

    Yes, that what I want to do

    //CUSTOM SEARCH BOX E SUO SHORTCODE
    //CODICE GURU di WordPress Buddha

    function wpb_demo_shortcode() {
    global $wpdb, $wp_query;

    // Retrieve categories dropdown
    $categories = wp_dropdown_categories([
    'show_count' => 1,
    'hierarchical' => 1,
    'show_option_all' => esc_html__('All destinations', 'sacconicase'),
    'echo' => false,
    ]);
    //fine categoria
    Moderator bcworkz

    (@bcworkz)

    You can use the “list_cats” filter to alter each category’s name when the locale is for German or English. You’d need to get the translated name from term meta, similar to what you’ve done elsewhere. You can get the current term’s ID from the WP_Term object that is passed as the second parameter.

    Thread Starter sacconi

    (@sacconi)

    Moderator bcworkz

    (@bcworkz)

    Well, it’s a start. I’d advise that you not pass an entire function to add_filter(), just pass the function’s name. Example:
    add_filter('list_cats', 'modify_list_cats_defaults', 10, 2);

    Your callback function must collect two terms, the term’s name and object:
    function modify_list_cats_defaults( $name, $object ) {

    The term_id property does not come from get_queried_object(), it comes from $object:
    $name = get_term_meta( $object->term_id, 'function_tipologia_de', true );

    You don’t really need the else conditions for either of the if conditions, though it’s OK to leave them. You could just do return $name; at the end, after both conditionals close. Note that I did not use $WP_Term like you did because the assigned value is not a WP_Term object, only its name. Name the variables as you wish, but be consistent in naming through out the function.

    You could make this work for any language by extracting the language code from the locale and using it to define the correct term meta key. The key name is the only difference in the code, everything else functions the same way. You’ve done this before elsewhere, but I don’t recall for what.

    Thread Starter sacconi

    (@sacconi)

    I have done this: https://pastebin.com/x44u0HMS but it’s not working. If I put the code in functions.php the shortcode that rules the searchbox doesnt work, if I put the code down below the ricerca.php file, that rules the search criteria, the search box is visible but the category terms disappear. So, also the position of the code is important

    Moderator bcworkz

    (@bcworkz)

    The code is flawed. Try this in functions.php:

    add_filter('list_cats', 'modify_list_cats_defaults', 10, 2);
    function modify_list_cats_defaults ( $name, $object ) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
         $name_de = get_term_meta( $object->term_id, 'function_tipologia_de', true );
         if ( ! empty( $name_de )) {
            return $name_de;
         }
       }
       return  $name;
    }
    Thread Starter sacconi

    (@sacconi)

    it doesnt work, I still see the terms in italian….

    Moderator bcworkz

    (@bcworkz)

    ˉ\_(ツ)_/ˉ Works for me. You use wp_dropdown_categories() to display the field? You have translations saved under the key “function_tipologia_de” in term meta? Flush your browser’s cache maybe?

    Thread Starter sacconi

    (@sacconi)

    Ok, I found the solution, the meta field was in reality “category_de”, not “function_tipologia_de”, I made a mistake when I opened the topic, now it’s working…

    Moderator bcworkz

    (@bcworkz)

    Great! You’re getting better at debugging ?? It’s not something easily learned in a class, it really takes practical experience.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.