2016-07-07 94 views
0

爲什麼我的自定義函數在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">&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標籤之前。

+0

你能告訴我你使用這個模板的內容嗎?我的意思是在博客或登錄等。或者你改變這在默認模板? –

+0

'你能告訴我你使用這個模板嗎?'提交一份表格。就像在「聯繫我們」頁面上一樣。這是一個自定義頁面。 – laukok

回答

0
<?php 
    if(isset($_POST['submitbuttonname'])) 
     your_function_name(); 
?> 

試試這可能適用於你。在你的代碼中,這個函數被調用兩次,第一次在頁面加載時調用,另一次調用它。所以給出條件,如果表單被提交然後調用該函數。

0

在此之後guide,只是把我的代碼之前:

<?php 
get_header(); ?> 

那麼問題就迎刃而解了。

相關問題