1
我已經看過這個時間比我可能計數,但不能爲我的生活弄清楚我做錯了什麼。過去我曾多次寫過短代碼函數,而且我的數量也超過了我的數量。無論出於何種原因,這個人拒絕正常工作。經過一個小時的反覆嘗試,我沒有找到成功。請幫忙。簡碼API參數拒絕像國會分支的邏輯
默認值出現在html中就好了,但不會反映短代碼中設置的參數。
- 我已經嘗試將ID作爲字符串和整數處理。
- 也許wordpress不喜歡我試圖使用attr
ID
...試過listID
,仍然失敗。 - 我嘗試過使用
extract
vs將shortcode_attr()
的結果保存到變量中。 - 想到也許一個插件或post_type阻止我獲勝,但是從Wordpress.org獲取示例短代碼API代碼並將其插入到functions.php並將其短代碼放入我的帖子後,它就起作用了。 我要瘋了
- 我已經清除了對象緩存,爲了調試這個,我改變了函數中的默認值,這些默認值在更改後立即出現在客戶端。
- 也許Shortcode API不喜歡我構建字符串。保存成使用
ob_start()
和ob_get_clean()
緩衝(喜歡洗澡?)...... 沒有喜悅 - 沒有,
var_dump
和print_r
無能爲力了我,沒有輸出。甚至當我嘗試var_dump(array('foo' => 'bar'))
或print_r(array('foo' => 'bar'))
現在我的理智被測試超出合理的措施,我幻想着DDOSing馬特·查爾斯·穆倫維格的智能手機
請治好我的瘋狂,和點出微小細節我顯然忽略了,所以我可以繼續我的一天。
簡碼
[subscription ID="1" referrer="overlay-promotion" buttonText="Send it to me" buttonBGHex="#000c49"]
功能
function subscription_form($atts){
//Yes, I tried extract to, and yes, I changed the variables in the html below to reflect this change.
$a = shortcode_atts(array(
"ID" => "1",
"referrer" => "not-what-you-think-this-should-be",
"buttonText" => "Subscribe",
"buttonBGHex" => "#78C138"
), $atts, "subscription");
$html = '<form id="subscribe" name="email_signup" class="form-inline validate" action="http://www.example.com/subscribe" method="post">';
$html .= '<input type="hidden" name="referrer" value="'.$a["referrer"].'">';
$html .= '<input type="hidden" name="success_url" value="http://www.example.com/subscribe/success">';
$html .= '<input type="hidden" name="error_url" value="http://www.example.com/subscribe/error">';
$html .= '<input type="hidden" name="list_id" value="'.$a["ID"].'">';
$html .= '<input type="text" placeholder="Enter your email address" class="input-large" name="email">';
$html .= '<input type="submit" class="btn btn-primary" style="background-color:'.$a["buttonBGHex"].';" name="submit" value="'.$a["buttonText"].'">';
$html .= '</form>';
return $html;
}
add_shortcode('subscription', 'subscription_form');
輸出
<form id="subscribe" name="email_signup" class="form-inline validate" action="https://www.example.com/subscribe" method="post">
<input type="hidden" name="referrer" value="HP-overlay-2015">
<input type="hidden" name="success_url" value="https://www.example.com/subscribe/success">
<input type="hidden" name="error_url" value="https://www.example.com/subscribe/error">
<input type="hidden" name="list_id" value="1"><input type="text" placeholder="Enter your email address" class="input-large" name="email">
<input type="submit" class="btn btn-primary" style="background-color:#78C138;" name="submit" value="Subscribe">
</form>
如果你'var_dump($ atts);'在運行'shortcode_atts'之前是否包含正確的信息或者它是一個空數組? – naththedeveloper
我絕對應該把它放在我嘗試過的東西的列表上,不幸的是,它不會輸出任何東西......就像所有的東西一樣。我也嘗試了print_r爲了好的措施...也沒有。我沒有做這個特別的WordPress安裝,並且我沒有訪問服務器配置(不幸)。 – Sandwich
根據API:「短代碼屬性名稱在傳遞到處理程序函數之前總是轉換爲小寫字母,值不受影響。」 - 所以你的屬性名稱可能是'id','buttontext'等。 – naththedeveloper