1
<?php
if(get_field('member_only_content')){
    echo "do something";
}else{
    echo "do something else";
}
?>

How do I insert HTML code where it says "do something"? When removing echo "do something"; and pasting the HTML code the Wordpress site crashes.

1
  • You should only replace the content within the string, you should not be removing the echo command as echo is what tells PHP to show the string to the browser. If you have a double-quote within the string then you will need to escape it using a backslash "like \"this\"" Commented Jul 15, 2012 at 15:11

7 Answers 7

5

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;
Sign up to request clarification or add additional context in comments.

Comments

2

Like this!

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

Comments

2

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>
    <?
}
     ?>

Comments

1

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

Comments

1

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

Comments

0
<?php
if(get_field('member_only_content')){
    echo "<div style='background-color: #888888'>This is a simple string between div's, make sure you've to be careful while inserting too many single and double quotes in this string as well as inline styles</div>";
}else{
    echo "do something else";
}
?>

Be careful while inserting quotes in your string.

Comments

0

Sample code

echo("<h1>This is a sample</h1>");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.