• 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 15 replies - 1 through 15 (of 25 total)
  • Moderator bcworkz

    (@bcworkz)

    The first assignment to $output line should be more like this:
    $output= '<div><input type="number" value="'. round( $number/7, 0 ) .'" placeholder="prezzo/notte in euro" /></div>';

    Similar for the next using $number2.

    The second echo line is missing the $ for variable $output.

    The final 2 else lines each need a terminal ;

    Thread Starter sacconi

    (@sacconi)

    Ok, this is fine!

    I have now to correct another function related to the above one:

    <form><?php echo '<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" >'; ?><input type="button" class="prezzo" value="da <?php echo round( (int)get_post_meta( get_the_ID(), "function_prezzo_primo", true )/7, 0 ); ?> euro/notte"></a></form></div>  

    I have to put a conditional. Now if “function_prezzo_primo” is not empty, print its value; elsif “function_prezzo_primo_dom” is not empty, print its value. If they both are empy it’s ok to output “0” like now. And the form is still visible. This is in content.php.

    I’d try something like this:

    <?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 );
    
    if ( !not empty( $listino)) {
    
    $output = //html of the output;
    
    } elsif ( !not empty( $listino2)) {
    
    $output = //html of the output;
    }
    
    } else $output = //html of the output;
    
    echo "$output";
    
    ?>

    I have some problems in ordering the output, in particular the anchor. Also the last else is not too clear

    Moderator bcworkz

    (@bcworkz)

    I’m not sure I fully understand your desire. In general one way to handle situations like this is before any conditionals are encountered, assign to $output the initial part that should always be there. For example:
    $output = '<form><a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" >Jump to somewhere on this page</a>';
    (not sure why there’s a link inside a form, seems odd to me, but it’s not technically wrong)

    Then in the conditionals, concatenate additional fields to $output depending on the condition. Maybe something like

    if ( ! empty( $listino)) {
        $content .= '<input name="foo" class="bar" value="'.$listino.'" placeholder="foo">';
    } // else condition or more conditionals as needed...

    “foo” and “bar” are just generic example attribute values, use something meaningful and useful instead.

    Finally, concatenate anything needed to close out what was started in the initial unconditional part. Then echo out the entire thing:

    $content .= '</form>';
    echo $content;
    • This reply was modified 2 years, 8 months ago by bcworkz. Reason: add missing quote
    Thread Starter sacconi

    (@sacconi)

    I’m here, but it’s full of mistakes

    $output = '<form><a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ></a>';
    
    $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 );
    
    if ( ! empty( $listino)) {
        $content .= '<input type="button" class="prezzo" value="da . $listino . euro/notte " >';
    } elseif  ( ! empty( $listino2)) {
    
     $content .= '<input type="button" class="prezzo" value="da . $listino2 . euro/notte " >';
    }else{ $listino2='';
    }
     else { $listino='';
    }
    $content . = $output .  $content . '</form>';
    echo $content;
    Moderator bcworkz

    (@bcworkz)

    You have too many else conditions. Maybe instead of elseif you need to nest within a separate if/else structure?

    if ( ! empty( $listino)) {
       // more code here
       if ( ! empty( $listino2)) {
           // more code here
       } else {
          $listino2 = '';
       }
    } else {
       $listino = '';
    }

    In the final concatenation to $content, you cannot have a space between . and =

    Thread Starter sacconi

    (@sacconi)

    The following code doesnt breack the site but the button with its content and its link doesnt output, it’s invisible

    $output = '<form><a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" ></a>';
    
    $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 );
    
    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;
    Moderator bcworkz

    (@bcworkz)

    You don’t have any link text between the anchor tags. Empty tags do not display.
    <a href="#whatever">Link text</a>

    Your input fields are not concatenated correctly, they are missing quotes separating strings from variables.
    $content .= '<input type="button" class="prezzo" value="da ' . $listino . ' euro/notte " >';
    Similar for the other field.

    In the last assignment before echoing, just assign, do not concatenate to $content because $content is what’s being concatenated. This will cause double fields.
    $content = $output . $content . '</form>';

    Thread Starter sacconi

    (@sacconi)

    Now I can see the button (2 buttons, not one), the price doesnt come out in the button, and the conditional is not working. Many faults, even if, for Php checker, I dont have syntax errors šŸ™‚

    $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;
    Moderator bcworkz

    (@bcworkz)

    It does not look like you’ve addressed the errors outlined in the second paragraph of my last reply regarding missing quotes. The same problem also occurs in the anchor link.

    I know the line I suggested looks just like what you have, but there’s a subtle, important difference. I draw your attention specifically to this part of the line:
    da ' . $listino . ' euro
    Notice the two single quotes that you don’t have. I also added spaces, the spaces are optional, the quotes are required.

    Thread Starter sacconi

    (@sacconi)

    I corrected the code, also in the anchor link:

    $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;
    

    but I got this: https://ibb.co/86VK2p9

    Moderator bcworkz

    (@bcworkz)

    If you’re seeing PHP code in your output there’s probably <?php ... /> tags missing somewhere. What you’ve posted is 100% PHP, so the PHP tags would be before and maybe after.

    Thread Starter sacconi

    (@sacconi)

    You were right, I put php tags, but it was not enough, I worked and changed the function, now it’s a little better, but it needs something more: https://test.sacconicase.com/case-vacanza/italia/toscana/marina-di-massa-appartamenti-vacanze/ , the apartment ID 12667 is $listino and the apartment ID 12652 is $listino 2, they both shoud output “da 100 euro/ notte, with the green button and an active link.

    my last work:

    <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 . $listino2 . ' 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>          

    for the other apartments the correct output should be “da 0 euro/notte” with only one “0”, and a green button and a link to the pricelist, as usual (the link is working, but no button)

    Moderator bcworkz

    (@bcworkz)

    Why are the two $listinos both run together in the anchor link text? The values look like one large number. This part: da ' . $listino . $listino2 . ' euro/notte. If the meta values are correct you get link text like “da 100100 euro/notte”. You likely need a separator between them? $listino . '-' . $listino2

    Anyway, other than that I don’t see any problem. I even tested your code on my site, only altering the meta key names and IDs to fit my data. What could be wrong then? Either the current post ID is unexpected or the meta keys are off or your underlying data is unexpected.

    You can check the ID with $output .= '<br>current ID:'.get_the_ID().'<br>'; placed after the link code. The underlying data can be checked through the phpMyAdmin app which gives you direct access to the DB.

    Thread Starter sacconi

    (@sacconi)

    I removed

    $listino2 .

    from $output but $listino2 is never output anywhere. The function doesnt do what is supposed to do. I should see in the first post “da 100 euro/notte” in the green button, now I see it twice (just one has a green button): https://test.sacconicase.com/case-vacanza/italia/toscana/marina-di-massa-appartamenti-vacanze/. And in the second post I should see “da 100 euro/notte” in the green button, now I cant see it . Output of $listino and $listino2 are the same because I assigned the same price to both apartments

    Moderator bcworkz

    (@bcworkz)

    It’s probably a good idea to assign different values just so if something goes wrong you have a better idea of where to look for the problem.

    What do you want the link text to actually read?. Did you do anything with $listino2 where it’s concatenated to $content? Something’s not right with one or the other $contents if one of the buttons are missing.

    Post your current code again, maybe I’ll be able to spot the problem.

Viewing 15 replies - 1 through 15 (of 25 total)

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