有点棘手的问题我无法找到任何适用的逻辑,或者我可能只是遗漏了一些东西, 我创建了一个单独的文件来定义我的函数,然后我将它包含在顶部,以便我可以随时随地运行函数。
所以在function.php
我有一个index.php
,其中我包含了其他几个文件,例如
otherred.php
otherblue.php
etc.php
etc.php
我有一个
的功能<?php
function my_revboxes($prv, $rnm, $postid, $mdetect){
if(isset($prv) && isset($rnm) && isset($mdetect)) {
if ($mdetect == 'redrav'){
$midv = 'pcom';
}
if ($mdetect == 'bluerav'){
$midv = 'pcomr';
}
if($prv !="" && $prv != "Text here" && $rnm !="") {
$usermeta = $rnm;
$posrav=get_post_meta($postid,$midv,true);
$posrav=$prv;
add_post_meta($postid,$midv,$posrav);
add_meta_user($usermeta, $posrav);
}
}
?>
<form method='POST' action="">
<textarea name=posrav value="" required placeholder ="If you are Agree please write your thoughts here"></textarea>
<?php
my_custom_field();
file_nameecho(); //detects if its otherred.php or otherblue.php
?>
<input type=submit value=submit>
</form>
</div>
<?php
}
此功能用于文件otherred.php
和otherblue.php
函数file_nameecho();
检测其是否为otherred或otherblue并相应地生成$mdetect
因此它可以将注释放在右侧(红色侧或蓝色侧),一切正常并且正常;奇妙。
但唯一的问题是该功能正在运行两次,因为它包含在红色和蓝色两个文件中,而且这两个文件都包含在主文件中。
我不想为每个人创建单独的功能,是否可以应用任何逻辑,它只执行一次,或者只在给定页面上执行或根据$mdetect
执行?
尝试了include_once和require_once仍无效。