在wordpress文本小部件中执行php代码

在wordpress文本小部件中执行php代码,php,wordpress,Php,Wordpress,我试图在文本小部件中执行一个if条件。我把它添加到我的主题函数文件中 function execute_php($html){ if(strpos($html,"<"."?php")!==false){ ob_start(); eval("?".">".$html); $html=ob_get_contents(); ob_end_clean(); } return $html; add_filter('widget_te

我试图在文本小部件中执行一个if条件。我把它添加到我的主题函数文件中

function execute_php($html){
 if(strpos($html,"<"."?php")!==false){
      ob_start();
      eval("?".">".$html);
      $html=ob_get_contents();
      ob_end_clean();
 }
 return $html;   
add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}
函数执行\u php($html){
if(strpos($html,“.”$html);
$html=ob_get_contents();
ob_end_clean();
}
返回$html;
} 添加过滤器('widget_text','execute_php',100)

但是当我写一个像这样的php条件时,它在第90行抛出一个错误,eval(“?”?““?”$html),下面是确切的错误

解析错误:语法错误,第13行的/functions.php(90):eval()代码中出现意外的“{”

这是我正在写的条件

    <?php if (is_post(43)) { ?>
 <div class="book">
  <a class="button" href="/product1">Buy Now</a>
 </div>
<?php } elseif (is_post(37)) { ?>
  <div class="book">
  <a class="button" href="/product2">Buy Now</a>
 </div>
<?php } elseif (is_post(30)) { ?>
 <div class="book">
  <a class="button" href="/product3">Buy Now</a>
 </div>
<?php } else (is_post(24)) { ?>
  <div class="book">
    <h5>Buy Now</h5>
    <select class="sportsman-colors" onchange="document.location.href=this.value">
     <option value="-1" selected>Choose Color</option>
     <option value="/product4">Green</option>
     <option value="/product6">Red</option>
    </select>
 </div>
<?php } ?>

立即购买
选择颜色
绿色
红色
有人能告诉我哪里出错了吗?我正在关闭所有的标签


谢谢

如果您想将php代码执行到wordpress小部件中,您可以使用插件式小部件逻辑,并在单独的文本小部件下添加条件,如
is_post(24)
,然后只需将以下代码更改为主题函数文件即可

function execute_php($html){
 if(strpos($html,"<"."?php")!==false){
      ob_start();
      eval("?".">".$html);
      $html=ob_get_contents();
      ob_end_clean();
 }
 return $html;   
add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}
add_filter('widget_text','execute_php',100);
函数execute_php($html){
if(strpos($html,“.”$html);
$html=ob_get_contents();
ob_end_clean();
}
返回$html;
}

你可以在

Hmm上阅读更多补偿,我希望修复我的代码,我确信我遗漏了一些东西。谢谢你的帮助。我会记住这个插件