du6jws6975
2015-11-22 13:25
浏览 67
已采纳

wordpress插入php解析错误

It does not work because the second block does not know what $out is. I did not find how I can handle

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

<html>
/*something */    

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

Edit: How can I write a for loop, it did not work

[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条回答 默认 最新

  • dongtuo2373 2015-11-22 13:38
    最佳回答

    The [insert_php]...[/insert_php] shortcodes are totally separate from each other. The entire body of that tag must be complete and valid PHP code.

    You can share the variable by using a global variable. I'd pick a name that's more unique than $out, to be on the safe side:

    [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>
    

    You can make a loop inside a single [insert_php]...[/insert_php] shortcode tag:

    [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]
    
    评论
    解决 无用
    打赏 举报