• sacconi

    (@sacconi)


    I need to make a code similar to this one: https://pastebin.com/pCRG7vze

    But the elements in the array come from custom taxonomies, each element of the taxonomy has already its custom field for german and english, so that I dont need to gettext them

    each taxonomy element is actually done this way: https://pastebin.com/uHRFk3XR

    How can I “mix” the 2 codes?

    The purpose is having each term separated by a comma, but the final element has a dot

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

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

    (@bcworkz)

    Move the term code to somewhere above the “variante” code. Insert the following where ever you want the term name to appear in the variante list (somewhere above the implode() line):
    $output_distanze[] = $term_name17_str;

    Thread Starter sacconi

    (@sacconi)

    I’m doing something wrong because I’m getting as output only the last term ($term_name18_str) and both with a comma and a dot: https://pastebin.com/G5hkNYfF

    I’m trying to concatenate 2 terms: $term_name17_str and $term_name18_str , after the first, a comma, after the second and last one, a dot.

    Thread Starter sacconi

    (@sacconi)

    Probably I need just $term_name17 and not $term_name17_str because the comma is written by the “implode”, but I still need to correct something

    Moderator bcworkz

    (@bcworkz)

    Yes, use $term_name17. With implode(), every element in the passed array is separated by the first arg, ', ' in this case. Then the terminal dot is concatenated to the imploded string.

    If you’re only getting one value as a result but with an extra comma, the other value is blank or an empty string. In the case of missing or empty values, you should not add them to the array at all. Maybe do something similar to the following for every array element:
    if ( ! empty( $term_name17 )) $output_piano_sup[] = $term_name17;

    Thread Starter sacconi

    (@sacconi)

    I have 2 problem: the first one is little, I have a blank space before the final dot in the array output: https://test.sacconicase.com/forte-dei-marmi-villa-con-giardino-disposta-su-2-piani-per-5-persone/

    If you see” Al 1° piano” (at the first floor), in italian there are 3 elements of the array. The second problem is that in the other languages I have just 2 elements in the array, even if I have correcty created the specific meta fields for english and german, maybe can you inspect the code for the field in enlglish? https://pastebin.com/dc14gNzE

    it is connected with this

    //bagno sup

    $lang = substr( get_locale(), 0, 2 );
    $terms = get_the_terms( get_the_ID(), 'bagno_sup');

    if ( is_array( $terms )) {
    $term = $terms[0]; // get the zeroth element in array, a WP_Term object
    if ( $lang == 'it' ){$term_name19 = $term->name . ' ' ;

    } else {$term_name = get_term_meta( $term->term_id , 'bagno_sup_'.$lang, true ). ' ' ;

    }
    } else {
    $term_name19 = '';
    }

    //fine bagno sup
    Thread Starter sacconi

    (@sacconi)

    Another thing about the implode: when there isnt any term I should have a dot as output

    Moderator bcworkz

    (@bcworkz)

    The reason German and English term meta are not appearing is because you have inconsistent variable names in your code. You use $term_name19 in a number of places but $term_name for non-Italian term meta. It too should be $term_name19.

    The reason a space is appearing after phrases is because you’re concatenating the space in your code. For example: $term_name19 = $term->name . ' ' ;
    You don’t need the . ' ' part.

    Let implode() handle what occurs between phrases. Let what happens at the end be handled after the implode() call. For example:
    echo implode(', ', $output_distanze ).'. ';

    I’m unclear about when you want just a dot to appear. When any single term is missing, or when there are no terms in the entire array to implode? For any single missing term you could do something like:
    $output_piano_sup[] = empty( $term_name17 ) ? '.': $term_name17;
    This could result in output similar to:

    Al 1° piano: ., ., 1 bagno con vasca.

    Seems odd to me. If this is not your intention, then what sort of output do you intend?

    • This reply was modified 2 months ago by bcworkz. Reason: code formatting
    Thread Starter sacconi

    (@sacconi)

    First 2 problems have been solutioned.

    About the implode() call: If there is at least one term my code is working properly, the problem arises when none of the terms are used. Because the dot at the end always appears.

    Moderator bcworkz

    (@bcworkz)

    Ah, I see what you mean now! Do something like this:
    if ( ! empty( $output_distanze )) echo implode(', ', $output_distanze ).'. ';

    • This reply was modified 1 month, 4 weeks ago by bcworkz. Reason: code format fixed
    Thread Starter sacconi

    (@sacconi)

    At the moment it’s not working, I have tryed a lot of positions but maybe the code isnt in the good one: https://pastebin.com/g4NYgzqP

    Moderator bcworkz

    (@bcworkz)

    My earlier suggestion was generic and did not reflect your specific context. Specifically what I suggest is you remove this part of the return statement: implode(', ', $output_piano_sup ).'.' . ' '
    and move it to above the return statement. Replace where it was with a unique variable not used elsewhere, maybe $piano_sup perhaps?

    Then with the implode() code that’s now above the return statement, alter it to be like this:
    $piano_sup = empty( $output_distanze )) ? '' : implode(', ', $output_distanze ).'. ';

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