• sacconi

    (@sacconi)


    Starting from

    return  $category.' '.$terms .' ' . __( 'for','sacconicase' )  .' '. $numero   .' '. __( 'persons, ','sacconicase' ). $mq . ' m2, ' . __( 'distance from the sea: ','sacconicase' ).  $sea   . ' m., ' . __( 'distance from the beach: ','sacconicase' ). $beach . ' m., ' . __( 'distance from the center: ','sacconicase' ) . $center . ' m., ' .$content;
    }
    

    there are some $ that should be printed only if it’s filled the field of a specific $. Let’s do an example: __( 'distance from the sea: ','sacconicase' ). $sea . ' m., '

    Here “distance from the sea” and ” m.,” should be printed only if the field of $ is not empty. This return is inside a shortcode

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

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

    (@bcworkz)

    I recommend establishing the return value solely with variables so you can use a double quoted string like I suggested in another topic of yours. IOW the return statement should have no function calls, not even translation functions like __(). Just string chars and variables.

    I don’t know how you get the value of $sea. It doesn’t really matter for this discussion. Let’s just assume it has been done. $sea at this point may or may not have a value. You can then do a conditional based on whether $sea is empty or not. This kind of condition is where the “ternary” operator is useful. It’s essentially a shorthand if/else statement. Like this:
    $sea_str = empty( $sea ) ? '' : __( 'distance from the sea: ','sacconicase' ). "$sea m.";
    Then your return line could be something like:
    return "$mq m^2 $sea_str";
    As a simplified example. You’d need to return much more than that but I hope you understand the concept.

    Thread Starter sacconi

    (@sacconi)

    Ok, it seems it works, I’m eliminating step by step all the functions from the return and keep only variables, at the moment I’m still using the single quotes in the return and it seems it works. Is it a deprecated practice? I have also another challenge. Let’s imagine I have just the following variables:

    $beach = get_post_meta( $post->ID, 'function_spiaggia', true );
    $beach_str = empty( $beach ) ? '' : __( 'distance from the beach: ','sacconicase' ). "$beach m., ";
    
    $center = get_post_meta( $post->ID, 'function_centro', true );
     $center_str = empty( $center ) ? '' : __( 'distance from the center: ','sacconicase' ). "$center m., ";  
    
    $shops = get_post_meta( $post->ID, 'function_negozi', true );
    $shops_str = empty( $shops ) ? '' : __( 'distance from the shops: ','sacconicase' ). "$shops m., ";  

    And I have in the return . $beach_str . $center_str . $shops_str .

    The last one VISIBLE, should have a point, not a comma. But I dont know in advance what is printed as last. If "$shops m., " were the last, i.e., it should be "$shops m.. "

    • This reply was modified 1 year ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    Neither the single quote or double quote style is deprecated. Both are entirely valid. Use the one you prefer. I personally like the double quote style, but it’s not for everyone.

    Yeah, the comma vs. dot issue with a variable number of empty strings is tricky to resolve. The best way I know of is to collect each variable into an array, then implode() the array using “, ” as the “glue” or separator. This function will not place a “, ” after the last item. Then you can append a “.” to whatever is returned.

    To build an array, first initialize an empty array: $output = array();

    Then after you get each meta value you’d do something like this:
    ! empty( $beach ) ? $content[] = __( 'distance from the beach: ','sacconicase' ). "$beach m., ";
    Instead of assigning to $beach_str.

    Or if you prefer
    if ( ! empty( $beach )) { $content[] = __( 'distance from the beach: ','sacconicase' ). "$beach m., ";}
    They are equivalent.

    Then in the end you can do:
    `return implode( $content, ‘, ‘).’.’;

    Thread Starter sacconi

    (@sacconi)

    In the return I have to use that special slanted apostrophe? how can I concatenate this part imploded with other parts non imploded or imploded but they are part of another group of variables? At the moment I have

    return '<span class="incipit_description">'. $category . ' '. $terms . '</span>' . ' ' . __( 'for','sacconicase' )  .' '. $numero   .' '. __( 'persons, ','sacconicase' ). ' ' . $mq . ' m2, ' .  $sea_str   . $beach_str  . $center_str . $shops_str  . $content;
    }
    

    $category and $terms will never need any comma or dot.

    Besides I’m already using the variable $content to call the content of a custom meta field, that is to say $content = get_post_meta( $post->ID, 'description_'.$lang, true );

    Moderator bcworkz

    (@bcworkz)

    Arrgh! I messed up the last line in my earlier reply. I meant:
    return implode( $output, ', ').'.';

    Doesn’t have to be $output, use anything that’s not already in use.

    Yes, in a double quoted string you still need to escape any quote characters with a \. For example: echo "Mr. O\'Brien is Irish";

    implode() will place a comma and space between every element in the array. So if you do not want certain text to be separated by comma and space, it all would need to be concatenated together prior to adding it to the array.

    Let’s say you do not want a comma and space between $beach_str and $center_str, but you do between $center_str and $shops_str. Then build the array like this:

    $output[] = $beach_str . $center_str;
    $output[] = $shops_str;
    return implode( $output, ', ').'.';
    Thread Starter sacconi

    (@sacconi)

    I’m going to try with the above code. In the meanwhile, in the same function the following is not working:

    if (!has_tag ('animali permessi su richiesta', $post)) { 
    $output2 = "__( 'Pets not allowed', 'sacconicase' )";
    
    }
    

    Gettext is the problem. The first part is working

    Moderator bcworkz

    (@bcworkz)

    If even a static string is not translated, then something is obviously wrong. I’ve outlined what needs to happen for gettext to work (.po and .mo files) in another of your topics. If the pets string appears in the .po file and it’s not translated while other strings are, I don’t know what the problem might be.

    Thread Starter sacconi

    (@sacconi)

    the problems is the php (not the translation) because the function outputs:

    per 3 persone, 50 m2, distanza dal mare: 450 m., distanza dalla spiaggia: 449 m., __( 'Pets not allowed', 'sacconicase' )

    php seems perfect, I get “No issues detected.”, but it is not somewhere

    Thread Starter sacconi

    (@sacconi)

    I renamed $output by $output_distanze, but it’s not working: https://pastebin.com/X0DP0Z62 , it doesnt output the distances as it should

    Moderator bcworkz

    (@bcworkz)

    All the distances need to be assigned to $output_distanze[] instead of $content[]

    Thread Starter sacconi

    (@sacconi)

    Message of PHP error:

    Deprecated implode(): Passing glue string after array is deprecated. Swap the parameters
    wp-content/themes/sacconicase/functions.php:815

    Moderator bcworkz

    (@bcworkz)

    Oops! Sorry to have mislead you, should be: implode(', ', $output_distanze )

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘return some $ under condition’ is closed to new replies.