国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > CMS > Wordpress > 正文

WordPress開(kāi)發(fā)中短代碼的實(shí)現(xiàn)及相關(guān)函數(shù)使用技巧

2024-09-07 00:48:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這篇文章主要介紹了WordPress開(kāi)發(fā)中短代碼的實(shí)現(xiàn)及相關(guān)函數(shù)使用技巧,文中講到了add_shortcode函數(shù)和shortcode_atts函數(shù)的用法,需要的朋友可以參考下。

其實(shí)實(shí)現(xiàn)短代碼很簡(jiǎn)單,我們只需要用到 WordPress 里面的一個(gè)函數(shù)就可以搞定短代碼,外加自己的一個(gè)小函數(shù),可以讓短代碼實(shí)現(xiàn)的輕松加愉快。

短代碼實(shí)現(xiàn)原理

就像往 WP 一些動(dòng)作里加鉤子和過(guò)濾函數(shù)一樣,短代碼只是經(jīng)過(guò)封裝了的針對(duì)文章輸出內(nèi)容的過(guò)濾器而已,沒(méi)有像有一些主題功能說(shuō)的那么震撼、那么高深。

下面來(lái)一個(gè)簡(jiǎn)單例子:

  1. function myName() {//短代碼要處理的函數(shù) 
  2. return "My name's XiangZi !"
  3. //掛載短代碼 
  4. //xz為短代碼名稱  
  5. //即你在編輯文章時(shí)輸入[xz]就會(huì)執(zhí)行 myName 函數(shù) 
  6. add_shortcode('xz''myName'); 

那么我們?cè)谖恼轮休斎隱xz]就會(huì)得到

My name's XiangZi !

短代碼傳參

更高深一點(diǎn)的利用,我將會(huì)在后面的文章中講到,今天只講一下,短代碼的傳參機(jī)制

高級(jí)一點(diǎn)的例子

  1. function myName($array,$content) { 
  2. var_dump($array); 
  3. var_dump($content); 
  4.    
  5. add_shortcode('xz''myName'); 

編輯文章時(shí)我們輸入:

[xz a="1" b="2" c="3"]這里是三個(gè)參數(shù)哦[/xz]

在函數(shù)中我們將得到:

  1. //$array 是一個(gè)數(shù)組, 
  2. //大體結(jié)構(gòu)如下 
  3. $array = array('a'=>'1','b'=>'2','c'=>'3'); 
  4. //$content 是一個(gè)字符串 
  5. $content = '這里是三個(gè)參數(shù)哦'
  6. shortcode_atts 

不是因?yàn)楦愣檀a插件,我也不會(huì)用到這個(gè)函數(shù),shortcode_atts 函數(shù)主要是用來(lái)設(shè)置短代碼中截獲變量的初始值。

這是一個(gè)很實(shí)用的函數(shù),其實(shí)這個(gè)函數(shù)的真正是作用在數(shù)組上得,因?yàn)槲覀儚亩檀a中截獲的參數(shù)都是數(shù)組形式的。

shortcode_atts 函數(shù)詳解

不要被函數(shù)名所疑惑,在 WordPress 里主要是用于設(shè)置短代碼參數(shù)的默認(rèn)值,如果我們將代碼提取出來(lái),用在別的地方,該函數(shù)可以幫我們?cè)O(shè)置一個(gè)既得數(shù)組的默認(rèn)值。

shortcode_atts 函數(shù)使用

這個(gè)函數(shù)使用起來(lái)很簡(jiǎn)單。

  1. shortcode_atts(array
  2. "url" => 'http://PangBu.Com' 
  3. ), $url

以上代碼的意思是,將 $url 數(shù)組 鍵值為url的成員默認(rèn)值設(shè)定為'http://PangBu.Com',別的地方用處似乎不多,但對(duì)于一些超級(jí)懶人,有時(shí)候攬到總是忘記或是懶得設(shè)定數(shù)組的數(shù)值時(shí),這個(gè)函數(shù)超好用。

shortcode_atts 函數(shù)聲明

  1. /** 
  2.  * Combine user attributes with known attributes and fill in defaults when needed. 
  3.  * 
  4.  * The pairs should be considered to be all of the attributes which are 
  5.  * supported by the caller and given as a list. The returned attributes will 
  6.  * only contain the attributes in the $pairs list. 
  7.  * 
  8.  * If the $atts list has unsupported attributes, then they will be ignored and 
  9.  * removed from the final returned list. 
  10.  * 
  11.  * @since 2.5 
  12.  * 
  13.  * @param array $pairs Entire list of supported attributes and their defaults. 
  14.  * @param array $atts User defined attributes in shortcode tag. 
  15.  * @return array Combined and filtered attribute list. 
  16.  */ 
  17. function shortcode_atts($pairs$atts) { 
  18.  $atts = (array)$atts
  19.  $out = array(); 
  20.  foreach($pairs as $name => $default) { 
  21.  if ( array_key_exists($name$atts) ) 
  22.   $out[$name] = $atts[$name]; 
  23.  else 
  24.   $out[$name] = $default
  25.  } 
  26.  return $out
  27. }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临泉县| 富裕县| 哈密市| 靖安县| 屯昌县| 八宿县| 丘北县| 江津市| 惠来县| 漾濞| 板桥市| 乐安县| 湖北省| 铜陵市| 武陟县| 大荔县| 织金县| 定远县| 庆元县| 密云县| 吉木萨尔县| 怀仁县| 南京市| 天气| 呈贡县| 延寿县| 蒲城县| 高唐县| 博客| 清远市| 霸州市| 贵定县| 宣武区| 句容市| 福安市| 富源县| 九江县| 太保市| 拉萨市| 林州市| 武山县|