【问题标题】:WordPress - Why my custom function being called twice?WordPress - 为什么我的自定义函数被调用两次?
【发布时间】:2016-07-07 13:41:25
【问题描述】:

为什么我的自定义函数在 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">
...

提交表单后,我的屏幕上出现了两次&lt;p class="alert alert-danger"&gt;&lt;span class="close" data-dismiss="alert"&gt;&amp;times;&lt;/span&gt; Work NOT submitted! Please correct the error(s) below.&lt;/p&gt;。一个是 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标签应该存在,但它确实存在!为什么?我该如何解决这个问题?

我希望消息出现在 form 标记之前,而不是 html 标记之前。

【问题讨论】:

  • 你能告诉我你用这个模板做什么吗?我的意思是在博客或登录等中。或者您是否在默认模板中更改此设置?
  • can you please tell me for what you use this template? 提交表单。就像在contact us 页面上一样。这是一个自定义页面。

标签: php wordpress wordpress-4.5.2


【解决方案1】:
<?php
   if(isset($_POST['submitbuttonname']))
      your_function_name();
?>

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

【讨论】:

    【解决方案2】:

    按照这个guide,把我的代码放在前面:

    <?php
    get_header(); ?>
    

    那么问题就解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多