• I’m trying to pass from this function:

    function prezzo_minimo_meta_box_render( $post ){
    
    // Get the number and display it in a numeric field
        $number = get_post_meta( $post->ID, "function_prezzo_primo", true );
        echo '<div><input type="number" name="function_prezzo_primo" value="'. round( $number/7, 0 ) .'" placeholder="prezzo/notte in euro" /></div>';
     
    }   
    

    to this other one:

    function prezzo_minimo_meta_box_render( $post ){
    
      $number = get_post_meta( $post->ID, "function_prezzo_primo",true );
    
     $number2 = get_post_meta( $post->ID, "function_prezzo_primo_dom" ,true);
    
    // Get the number and display it in a numeric field
    
    if( !empty( get_post_meta( $post->ID, "function_prezzo_primo", true ))) {
       
    
    $output= '<div><input type="number".  $number . 'round( $number/7, 0 ) .'" placeholder="prezzo/notte in euro" /></div>';
    
    echo "$output";
    
    } elseif (!empty(get_post_meta( $post->ID, "function_prezzo_primo_dom"))) {
    
    $output= '<div><input type="number".  $number2 . 'round( $number2/7, 0 ) .'" placeholder="prezzo/notte in euro" /></div>';
    
    echo "output";
    
    }  else {$output= ''
    
    }else {$output= ''
    
    }
    
    }

    I have problems with the syntax, at least about the round price

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

Viewing 10 replies - 16 through 25 (of 25 total)
  • Thread Starter sacconi

    (@sacconi)

    At the moment I have the following:

    <div class="prezzo">
    <?php
    $listino =  round( (int)get_post_meta( get_the_ID(), "function_prezzo_primo", true )/7, 0 );
    $listino2 =  round( (int)get_post_meta( get_the_ID(), "function_prezzo_primo_dom", true )/7, 0 );
    $output = '<form><a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" >da ' . $listino . ' euro/notte </a>';
    
    
    
    if ( ! empty( $listino)) {
        $content .= '<input type="button" class="prezzo" value="da ' . $listino . ' euro/notte " >';
     if ( ! empty( $listino2)) {
    
     $content .= '<input type="button" class="prezzo" value="da ' .  $listino2  . 'euro/notte " >';
    }else{ $listino2='';
    }
    } else { $listino='';
    }
    
    $content = $output . $content . '</form>';
    
    echo $content;
    
    ?>
    </div>          

    The link you can try clicking on “da 100 euro/ notte” (the above one, without green background), is correct: https://test.sacconicase.com/case-vacanza/italia/toscana/marina-di-massa-appartamenti-vacanze/

    Now it gets even more complicated because I’d need gettext on “da” and “euro/notte”, I tryed but..I got error. (I have wrapped successfully another function today, but it was easier)

    Moderator bcworkz

    (@bcworkz)

    I get a link and two buttons with your code, works perfectly for me. All I changed was the meta key names to fit my existing data.

    I think maybe the post has no “function_prezzo_primo_dom” meta value? In which case there would be no second button because that’s how the code is designed to work.

    BTW, I think you’d want a space in front of “euro” in the button field for $listino2, the button that is not visible on your site. The link and first button already have the space.

    Thread Starter sacconi

    (@sacconi)

    Now I understand, the is a misunderstanding. I dont need 2 outputs in the same post, it is alternatively or $listino or $listino2. The first price list is for the apartments rented from saturday to saturday, the second price list is for the apartments rented from sunday to sunday. there are 2 series of fields, but for each post/apartment just one is showed (there is no sense I fill the 2 series at the same time). I think it’s the logic to be changed

    Moderator bcworkz

    (@bcworkz)

    You could move the entire $listino2 conditional structure to outside the $listino conditional. Only save one meta value or the other. The if (! empty()) logic will suppress output if no value is saved.

    There’d be nothing preventing someone from saving two values though. You could alter the form so only one or the other can be saved, never both. For example, radio buttons for Sat.-Sun. or Sun.-Sun., followed by a single price field. The price gets saved according to the selected radio button.

    Thread Starter sacconi

    (@sacconi)

    Ok, I found the solution! (I think this is even better than before because actually it hasnt so much sense having an output with the button if no price is displayed)

    <?php
    $listino =  round( (int)get_post_meta( get_the_ID(), "function_prezzo_primo", true )/7, 0 );
    $listino2 =  round( (int)get_post_meta( get_the_ID(), "function_prezzo_primo_dom", true )/7, 0 );
    $output = '<form>';
    
    if ( ! empty( $listino)) {
        $content .='<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ><input type="button" class="prezzo" value="da ' . $listino . ' euro/notte " ></a>';
    
     } elseif ( ! empty( $listino2)) {
    
     $content .= '<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ><input type="button" class="prezzo" value="da ' .  $listino2  . ' euro/notte " ></a>';
    }
    else{ $listino='';
    $listino2='';
    }
    
    
    $content = $output . $content . '</form>';
    
    echo $content;
    
    ?>
    

    But there are still a couple of things to fix or develop:

    1. undefinite variables are not closed correctly
    2. I should gettext-wrap da ' . $listino . ' euro/notte (the words I need are “da” and “euro/notte”
    Moderator bcworkz

    (@bcworkz)

    I see what you did there šŸ™‚ Nice!

    I’m sorry, I don’t understand #1

    #2 – once again, break them into parts. Translate as needed. Concatenate back together again.

    You of course can translate “da” and “euro/notte” separately. OR you could use sprintf() style formatting to translate the entire phrase all at once. The numeric part would consist of a place holder that gets dynamically inserted on output.
    sprintf( __('da %d euro/notte', 'sacconicase'), $listino )

    Be sure any translation always keeps the placeholder: %d

    Thread Starter sacconi

    (@sacconi)

    #2 – I got an error here:

    $content .='<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ><input type="button" class="prezzo" value="sprintf( __('da %d euro/notte', 'sacconicase'), $listino ) " ></a>';

    #1 -In the above working function, probably the following:

    else{ $listino='';
    $listino2='';
    }

    is not placed in the right position. I have the usual problem of indefinite variables, I got warning errors

    Since the 2 conditions are not nested maybe have I to treat these 2 “else” separately?

    Moderator bcworkz

    (@bcworkz)

    You inserted the sprintf() clause into the middle of a static string. You need to tell the parser it’s not a static string by also inserting quotes and concatenation operators.
    $content .='<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ><input type="button" class="prezzo" value="'. sprintf( __('da %d euro/notte', 'sacconicase'), $listino ) .'" ></a>';

    What are the warnings you see? I tried your code with various combinations of $listino and $listino2 not being defined and never got any warnings. And on at what lines do the warnings say the error occurs?

    Thread Starter sacconi

    (@sacconi)

    Problems on line 50

    $content .='<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ><input type="button" class="prezzo" value="'. sprintf( __('da %d euro/notte', 'sacconicase'), $listino ) .'" ></a>';

    Notice Undefined variable: content+

    1. wp-content/themes/sacconicase/template-parts/content.php:50

    and also line 62, it is the end of the same function

    $content = $output . $content . '</form>';

    Notice Undefined variable: content+

    1. wp-content/themes/sacconicase/template-parts/content.php:62
    Moderator bcworkz

    (@bcworkz)

    Adding $content = ''; near the start of your code ought to resolve the warnings. This initializes the variable so other code can either concatenate to it or even overwrite it later on. It also avoids possible warnings should nothing else be concatenated or assigned.

Viewing 10 replies - 16 through 25 (of 25 total)

The topic ‘Conditional in meta box render’ is closed to new replies.