0

为什么我的自定义函数在 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">&times;</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">&times;</span> Work NOT submitted! Please correct the error(s) below.</p>我的屏幕上会出现两次。一个html 标签之前,另一个在正确的位置:

<p class="alert alert-danger"><span class="close" data-dismiss="alert">&times;</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">&times;</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 标记之前。

4

2 回答 2

0

按照本指南,只需将我的代码放在前面:

<?php
get_header(); ?>

然后问题就解决了。

于 2016-07-08T06:04:00.407 回答
0
<?php
   if(isset($_POST['submitbuttonname']))
      your_function_name();
?>

试试这个也许这对你有用。在您的代码中,该函数被调用两次,第一次是在加载页面时,另一次是在您调用它时。所以给出一个条件,如果表单被提交然后调用那个函数。

于 2016-07-07T14:06:23.917 回答