WordPress开发中短代码的实现及相关函数使用技巧
《:WordPress开发中短代码的实现及相关函数使用技巧》要点: PHP应用其实实现短代码很简单,我们只需要用到 WordPress 里面的一个函数就可以搞定短代码,外加本身的一个小函数,可以让短代码实现的轻松加愉快. 短代码实现原理 function myName() {//短代码要处理的函数 return "My name's XiangZi !"; } //挂载短代码 //xz为短代码名称 //即你在编纂文章时输入[xz]就会执行 myName 函数 add_shortcode('xz','myName'); 那么我们在文章中输入[xz]就会得到 My name's XiangZi ! 短代码传参 function myName($array,$content) { var_dump($array); var_dump($content); } add_shortcode('xz','myName'); 编纂文章时我们输入: [xz a="1" b="2" c="3"]这里是三个参数哦[/xz] 在函数中我们将获得: //$array 是一个数组,//大体布局如下 $array = array('a'=>'1','b'=>'2','c'=>'3'); //$content 是一个字符串 $content = '这里是三个参数哦'; shortcode_atts shortcode_atts 函数详解 PHP利用shortcode_atts 函数使用 shortcode_atts(array( "url" => 'http://PangBu.Com' ),$url) 以上代码的意思是, PHP利用shortcode_atts 函数声明 PHP利用
/**
* Combine user attributes with known attributes and fill in defaults when needed.
*
* The pairs should be considered to be all of the attributes which are
* supported by the caller and given as a list. The returned attributes will
* only contain the attributes in the $pairs list.
*
* If the $atts list has unsupported attributes,then they will be ignored and
* removed from the final returned list.
*
* @since 2.5
*
* @param array $pairs Entire list of supported attributes and their defaults.
* @param array $atts User defined attributes in shortcode tag.
* @return array Combined and filtered attribute list.
*/
function shortcode_atts($pairs,$atts) {
$atts = (array)$atts;
$out = array();
foreach($pairs as $name => $default) {
if ( array_key_exists($name,$atts) )
$out[$name] = $atts[$name];
else
$out[$name] = $default;
}
return $out;
}
欢迎参与《:WordPress开发中短代码的实现及相关函数使用技巧》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |