Skip to content Skip to sidebar Skip to footer

Insert HTML In If Statement

How do I insert HTML code where it says 'do something'? When r

Solution 1:

Close and open the PHP blocks, like this:

if(get_field('member_only_content')){
?>
    <html></html>
<?php
} else{
?>
    <html></html>
<?php
}

You can also use PHP's alternative syntax for this:

if(get_field('member_only_content')): ?>
    <html></html>
<?php else: ?>
    <html></html>
<?php endif;

Solution 2:

Like this!

<?php
if(get_field('member_only_content')) : ?>
    <div>Html stuff</div>
<?php else : ?>
    <div>More Html stuff</div>
<?php endif;

Solution 3:

Put HTML in place of the string.

<?php
if(get_field('member_only_content')){
    echo "<your>HTML here</your>";
}else{
    echo "<other>HTML</other>";
}
?>

You can also break out of the PHP tags

<?php
if(get_field('member_only_content')){
    ?> 
    <your>HTML here</your>
    <?
}else{

    ?>
     <other>HTML</other>
    <?
}
     ?>

Solution 4:

That would be because you placed the HTML code within php tags (<?php ?>). Text inside this tag is interpreted as a PHP instruction, thus it won't render HTML. There are 2 general ways render the HTML in a PHP file:

Echo the HTML

<?php

    if (get_field ('member_only_content')) {
        echo "<span>Put HTML here</span>";
    }
    else {
        echo "<span>Put HTML here</span>";
    }
?>

Place the HTML Outside PHP Tags

<?php if (get_field ('member_only_content')): ?>
    <span>Put HTML here</span>
<?php else: ?>
    <span>Put HTML here</span>
<?php endif;?> 

Solution 5:

Or you can use the <<< statement:

echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;

Straight from http://php.net/manual/en/function.echo.php


Post a Comment for "Insert HTML In If Statement"