WordPress PHP if else

I have to say that I’m a newbie when it comes to php , but still this code seems to make sense. I’m trying to display a form in 2 different ways – one way if it’s on the page ‘cotatie-seo’ and another way if it’s on any other page in the site. The code I’ve used is:

<div style="position: relative; float: left;">
<p>Nume (required)<br />
    [text* your-name] </p>

<p>Adresă Email (required)<br />
    [email* your-email] </p>

<p>Adresă website (required)<br />
[text* Website]</p>
<?php if(is_page('cotatie-seo')) { ?>
</div>
<div style="position: relative; float: left;">
<p>[textarea* observatii 30x10 id:observatii] </p>
</div>
<?php } else { ?>
<p>[textarea* observatii 30x10 id:observatii] </p>
</div>
<?php } ?>
<p>[submit "Trimite"]</p>

If it’s not on the cotatie-seo page it displays the form properly, with the text area under the rest of the fields, but when it’s on the cotatie-seo page the text area displays both in the first div and in the second div (so it is displayed twice, both under the rest of the fields and next to them).

Read More

Thank you in advance for the help!

Thanks for the fast reply! The function is the regular WordPress is_page function:

function is_page( $page = '' ) {    global $wp_query;

    if ( ! isset( $wp_query ) ) {       _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );         return false;   }

    return $wp_query->is_page( $page ); }

I’m sorry but I can’t post images and I’m building the site on localhost, so I can’t provide a preview of the output.

Cheers!

Related posts

Leave a Reply

1 comment

  1. One thing to try would be moving this bit:

    <p>[textarea* observatii 30x10 id:observatii] </p>
    </div>
    

    outside of the if-else (to just above the submit), and then dropping the else entirely since you repeat these lines in both if and else. Your snippet above would then be:

    <div style="position: relative; float: left;">
    <p>Nume (required)<br />
        [text* your-name] </p>
    
    <p>Adresă Email (required)<br />
        [email* your-email] </p>
    
    <p>Adresă website (required)<br />
    [text* Website]</p>
    <?php if(is_page('cotatie-seo')) { ?>
    </div>
    <div style="position: relative; float: left;">
    <?php } ?>
    <p>[textarea* observatii 30x10 id:observatii] </p>
    </div>
    <p>[submit "Trimite"]</p>