WordPress - 为什么我的自定义函数被调用两次?
问题描述:
为什么我的自定义函数在WordPress中被调用两次?WordPress - 为什么我的自定义函数被调用两次?
比如我在functions.php中此功能:
function your_function_name() {
if(isset($_POST['your_var'])) {
// return $error;
echo '<p class="alert alert-danger"><span class="close" data-dismiss="alert">×</span> Work NOT submitted! Please correct the error(s) below.</p>';
}
}
我的模板:
<?php
your_function_name();
?>
<form class="form-submission" method="post" action="">
<input type="text" class="form-control" placeholder="Name" name="your_var">
...
当提交表单时,我得到这个<p class="alert alert-danger"><span class="close" data-dismiss="alert">×</span> Work NOT submitted! Please correct the error(s) below.</p>
两次我的屏幕上。一个是之前的HTML标签,另一个是在正确的位置:
<p class="alert alert-danger"><span class="close" data-dismiss="alert">×</span> Work NOT submitted! Please correct the error(s) below.</p><!DOCTYPE html>
<html lang="en-US" class="no-js">
<head>
....
<p class="alert alert-danger"><span class="close" data-dismiss="alert">×</span> Work NOT submitted! Please correct the error(s) below.</p>
<form class="form-submission" method="post" action="">
<input type="text" class="form-control" placeholder="Name" name="your_var">
...
第一个以前 HTML标记应该不存在,但它确实!为什么?我怎样才能解决这个问题?
我希望消息出现在表格之前,而不是在html标签之前。
答
<?php
if(isset($_POST['submitbuttonname']))
your_function_name();
?>
试试这可能适用于你。在你的代码中,这个函数被调用两次,第一次在页面加载时调用,另一次调用它。所以给出条件,如果表单被提交然后调用该函数。
你能告诉我你使用这个模板的内容吗?我的意思是在博客或登录等。或者你改变这在默认模板? –
'你能告诉我你使用这个模板吗?'提交一份表格。就像在“联系我们”页面上一样。这是一个自定义页面。 – laukok