WordPress Shortcode的介紹
2016-06-24 07:45:47
12211
簡(jiǎn)單說(shuō) WordPress Shortcode 指的是一些使用[]包含的短代碼,WordPress會(huì)識(shí)別這些短代碼并根據(jù)短代碼的定義輸出為特定的內(nèi)容。
下面藍(lán)隊(duì)網(wǎng)絡(luò)為大家介紹一下它的類(lèi)型,基本概念,函數(shù),這三個(gè)方面
Shortcode 類(lèi)型
Shortcode API 支持幾乎所有可能的組合形式:自關(guān)閉標(biāo)簽,開(kāi)放標(biāo)簽,含有參數(shù)的標(biāo)簽等。
1
2
3
4
5
6 | [mycode]
[mycode foo="bar" id="123" color="red" something="data"]
[mycode]Some Content[/mycode]
[mycode]<p><a href="http://www.tjdsmy.cn/">HTML Content</a<>/p>[/mycode]
[mycode]Content [another-shotcode] more content[/mycode]
[mycode foo="bar" id="123"]Some Content[/mycode] |
Shortcode 基本概念
首先你要去定義一個(gè)函數(shù),來(lái)處理你定義的 Shortcode,和它的屬性參數(shù)以及引用的內(nèi)容。
1
2
3
4
5
6 | function my_shortcode_func($attr, $content) {
// $attr $key=>$value 的數(shù)組
// $content 是 shortcode 中包含的字符串
// 對(duì) $attr 和 $content 進(jìn)行處理
// 返回預(yù)期的值} |
然后把自己定的 Shortcode 和其處理函數(shù)管理起來(lái),以便 [mycode attr="value"]content[/mycode] 能夠按照預(yù)期執(zhí)行。
1 | add_shortcode('mycode', 'my_shortcode_func') |
Shortcode 相關(guān)的所有函數(shù)
WordPress 定義了以下和 Shortcode 相關(guān)的函數(shù):
1
2
3
4 | add_shortcode('mycode', 'function_name'); // 定義一個(gè)新的 Shortcoderemove_shortcode('mycode'); // 移除一個(gè) Shortcoderemove_all_shortcodes(); // 移除所有的 Shortcode$return = do_shortcode($content); // 應(yīng)用 Shortcode 到內(nèi)容而不輸出 |
會(huì)員登錄
賬號(hào)登錄還沒(méi)有賬號(hào)?立即注冊(cè)