wordpress插入php解析错误

时间:2015-11-22 13:25:01

标签: php wordpress insert

它不起作用,因为第二个块不知道$ out是什么。我没有找到如何处理

[insert_php]
/* do something */
$out = smthing;
[/insert_php]

<html>
/*something */    

[insert_php]
echo $out;
[/insert_php]
</html>

修改     如何编写for循环,它不起作用

[insert_php]foreach($elements as $element){ [/insert_php]
<label class="radio-inline"><input type="radio" name="opt" value=[insert_php]echo $element; [/insert_php] > [insert_php] echo $element; [/insert_php]</input> </label>
[insert_php]}[/insert_php]

1 个答案:

答案 0 :(得分:1)

[insert_php]...[/insert_php]短代码彼此完全分开。该标记的整个主体必须​​是完整且有效的PHP代码。

您可以使用全局变量共享变量。为了安全起见,我会选择一个比$out更独特的名称:

[insert_php]
global $MY_CUSTOM_TEMP_VAR;   
/* do something */
$MY_CUSTOM_TEMP_VAR = smthing;
[/insert_php]

<html>
/*something */    

[insert_php]
global $MY_CUSTOM_TEMP_VAR;  
echo $MY_CUSTOM_TEMP_VAR;
[/insert_php]
</html>

您可以在 [insert_php]...[/insert_php]短代码标记中创建一个循环

[insert_php]
foreach($elements as $element)
    echo "
       <label class='radio-inline'>
         <input type='radio' name='opt' value='" . esc_attr( $element ) ."'>$element</input>
       </label>
    ";
[/insert_php]