• On this page this part is still in italian. But the translations were working before.

    “L’appartamento si compone di: soggiorno con angolo cottura e divano letto doppio, 1 camera da letto tripla, 1 camera doppia, 1 bagno con doccia. 2 balconi, 1 posto auto”

    All the description is inserted by a shortcode but certains fields work and others dont

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

Viewing 15 replies - 16 through 30 (of 39 total)
  • Thread Starter sacconi

    (@sacconi)

    The taxonomy sidebar box: https://ibb.co/p1bDWwG , I’d like the taxonomy “introduzioni” to show all the terms, like the taxonomy “Tipologie” but even more, because “Tipologie” shows only the most used terms. How can I display 100% of terms? I dont need child terms, only parents

    Ok, i should put a conditional for foreigner terms here:

    $term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;

    $term_name is in the return of a concatenation (shortcode content)

    the field for german is: https://pastebin.com/dYWgDz4r

    Moderator bcworkz

    (@bcworkz)

    It’s difficult to modify taxonomy sidebar boxes. I recommend altering the code that registers tipologia taxonomy to make it hierarchical. Then the box will look more like the default categories box, listing all terms. Which you then select with check boxes.

    To translate ‘intro” terms, it’s the same process as done elsewhere.
    Get the locale and extract the language.
    Get post terms, extract the [0] element.
    If the language is not “it”, get term meta and assign to $term_name.
    Else, assign the term’s name to $term_name.

    Thread Starter sacconi

    (@sacconi)

    I tryed

    $lang = substr( get_locale(), 0, 2 );
    global $post;
    
    if ('it' != $lang ) {
    
    $term_name = get_the_terms(get_the_ID(), 'intro_'.$lang)[0]->name;
    } else
    
    $term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;
    

    and also with 'intro_.$lang' , and without global $post; but the german version is broken in the layout

    I made the taxonomy “intro” hierarchical and it’s ok, I see the terms to check. Any possibility to have this box in the main space instead than on the lateral sidebar?

    Thread Starter sacconi

    (@sacconi)

    Update

    Now

    $term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;

    has become

    $term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;
    $term_name_str = empty( $term_name ) ? '' : "$term_name: ";

    Moderator bcworkz

    (@bcworkz)

    If the goal is make a key name like “intro_de” using $lang, either of these will work:
    "intro_$lang"
    or
    'intro_'.$lang

    Any possibility to have this box in the main space instead than on the lateral sidebar?

    It’s possible for custom meta boxes, but the default ones for taxonomies are not so easy, at least in the block editor. In the classic editor we could drag the boxes about on the screen as we please. For default taxonomy meta boxes in the block editor I think you’d have to directly alter the global registration array $wp_meta_boxes in order to modify the box’s $context arg from ‘advanced’ to ‘normal’.

    Thread Starter sacconi

    (@sacconi)

    Unlickily this is not working for foreigner languages

    $lang = substr( get_locale(), 0, 2 );
    global $post;
    
    if ('it' != $lang ) {
    
    $term_name = get_the_terms(get_the_ID(), 'intro_'.$lang)[0]->name;
    } else
    
    $term_name = get_the_terms(get_the_ID(), 'intro')[0]->name;
    Moderator bcworkz

    (@bcworkz)

    get_the_terms() gets an array of term objects. You don’t use $lang to get terms assigned to a post because you don’t have foreign language taxonomies, only foreign language meta data. You can get object properties like their names directly. To get translated names from term meta, it takes an extra step. For example:

    $term = get_the_terms(get_the_ID(), 'intro')[0];
    if ('it' != $lang ) {
       $term_name = get_term_meta( $term->term_id, 'intro_'.$lang, true );
    } else {
       $term_name = $term->name;
    }

    I’m assuming the German translation is saved under meta key “intro_de”. I think your taxonomy is “intro”, or is it “introduzioni”? Coordinate the correct taxonomy name in get_the terms(). I’m not sure about meta keys, coordinate the correct key name in get_term_meta().

    Thread Starter sacconi

    (@sacconi)

    Ok, it works! Since ‘intro’ always exists and I need a colon I did

    $term = get_the_terms(get_the_ID(), 'intro')[0];
    if ('it' != $lang ) {
       $term_name = get_term_meta( $term->term_id, 'intro_'.$lang, true ) . ': ' ;
    } else {
       $term_name = $term->name . ': ';
    }
    

    Since many taxonomies sometimes may not be used and and I need a comma only when they exist, I did

    $term = get_the_terms(get_the_ID(), 'soggiorno')[0];
    if ('it' != $lang ) {
       $term_name2 = get_term_meta( $term->term_id, 'soggiorno_'.$lang, true ) ;
    
    $term_name2_str = empty( $term_name2 ) ? '' : "$term_name2, ";
    
    } else {
       $term_name2 = $term->name  ;
    
    $term_name2_str = empty( $term_name2 ) ? '' : "$term_name2, ";
    
    } 
    

    It seems it works but it is correct? It this is ok, now I should have a specific tag printed/selected in the tag box when a taxonomy term is checked, or a specific term or all the terms, as I did for the custom selector. Now the selector became a taxonomy

    • This reply was modified 2 years, 2 months ago by sacconi.
    Thread Starter sacconi

    (@sacconi)

    PHP ERRORS

    I got some php errors with my last code

    Trying to access array offset on value of type bool+

    1. wp-content/themes/sacconicase/functions.php:1617 in the first line of
    $term = get_the_terms(get_the_ID(), 'intro')[0];
    if ('it' != $lang ) {
       $term_name = get_term_meta( $term->term_id, 'intro_'.$lang, true ) . ': ' ;
    } else {
       $term_name = $term->name . ': ';
    }

    In this functions I got also another error: Trying to get property ‘name’ of non-object

    • This reply was modified 2 years, 2 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    If trying to get the zeroth element returned from get_the_terms() results in that error, then get_the terms() has failed to return anything. More robust code would verify the return is not false or WP_Error before attempting to extract the zeroth element. The current code assumes everything is correct and the current post has assigned “intro” terms.

    Is “intro” the correct taxonomy name? Or should it be “introduzioni”? If “intro” is correct, then either get_the_ID() is not returning a valid post ID or the post has no terms assigned.

    The non-object error is knock-on effect from the initial failure above. If the above error is corrected, then “name” will be a proper object property.

    Thread Starter sacconi

    (@sacconi)

    Here is my taxonomy “intro”: https://pastebin.com/ypeLAmz5

    So I get an error when I dont check any term of the taxonomy? Actually I tested the taxonomy just on some apartments/posts, maybe this explains why I get 220 PHP errors when I’m on an archive page

    I think I should add a little bit of code as I did for the “indefinite variable”, for the case “no term is checked”, but where? So let’s do more robust code 🙂

    I did a try on an apartment/post, I checked some taxonomy terms (before no terms were checked), and the total amount of PHP errors slowed down. The problem is that for each apartment I dont need to use all the taxonomies I have prepared.

    • This reply was modified 2 years, 2 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    It makes sense not all posts would have assigned terms. Even if they eventually would, they would not during development anyway. Thus more robust error checking is necessary.

    To check the return value, revise get_the_terms() line like this:
    $terms = get_the_terms( get_the_ID(), 'intro');

    get_the_terms() will either return false or WP_Error when no terms are assigned or it fails for some other reason. It will always return an array otherwise. So right after calling get_the_terms(), wrap everything that uses
    $term in this conditional:

    if ( is_array( $terms )) {
       $term = $terms[0]; // get the zeroth element in array, a WP_Term object
    
       // everything using $term goes here
    
    } else {
       $term_name = '';
    }
    Thread Starter sacconi

    (@sacconi)

    I did this but it doesnt work

    $terms = get_the_terms( get_the_ID(), 'intro');
    if ( is_array( $terms )) {
       $term = $terms[0]; // get the zeroth element in array, a WP_Term object
    
     if ('it' != $lang ) {
       $term_name = get_term_meta( $term->term_id, 'intro_'.$lang, true ) . ': ' ;
    
    } else {
       $term_name = '';
    }
    } 
    Moderator bcworkz

    (@bcworkz)

    The translation part works in my testing. Did you extract the language code from locale and assign it to $lang? It’s not in the snippet you posted. Not sure if it exists or not in your actual code.

    If it’s not done, you should be getting an undefined variable error.

    The Italian part does not work. There should be two else conditions. If $lang == ‘it’, you should be assigning $term->name to $term_name. If $terms is not an array, only then do you do $term_name = '';

    Thread Starter sacconi

    (@sacconi)

    I corrected some errors but I think I have many others:

    $lang = substr( get_locale(), 0, 2 );
    global $post;
    $terms = get_the_terms( get_the_ID(), 'intro');
    
    if ( is_array( $terms )) {
    
     if ( $lang == 'it' ){
       $term = $terms[0]; // get the zeroth element in array, a WP_Term object
    } 
    else {
     $term_name = get_term_meta($term->name , 'intro_'.$lang, true ) . ': ' ;
    } 
    } else {
       $term_name = '';
    } 
    
Viewing 15 replies - 16 through 30 (of 39 total)

The topic ‘Problems with gettext in custom fields’ is closed to new replies.