我想制作一个简单的快捷码,返回指定大小的youtube链接并使用内容。类似于:
[yt]http://youtu.be/Nl29v5pfxTw[/yt]
我有这个代码,但我想简化它:
function youtube($atts) {
extract(shortcode_atts(array(
"value" => \'http://\',
"width" => \'620\',
"height" => \'350\',
"name"=> \'movie\',
"allowFullScreen" => \'true\',
"allowScriptAccess"=>\'always\',
), $atts));
return \'<object style="height: \'.$height.\'px; width: \'.$width.\'px"><param name="\'.$name.\'" value="\'.$value.\'"><param name="allowFullScreen" value="\'.$allowFullScreen.\'"></param><param name="allowScriptAccess" value="\'.$allowScriptAccess.\'"></param><embed src="\'.$value.\'" type="application/x-shockwave-flash" allowfullscreen="\'.$allowFullScreen.\'" allowScriptAccess="\'.$allowScriptAccess.\'" width="\'.$width.\'" height="\'.$height.\'"></embed></object>\';
}
add_shortcode("yt", "youtube");
问题是,对于这段代码,我需要使用[yt value=”http://youtu.be/Nl29v5pfxTw“]我只想使用[yt]$内容
我如何才能做到这一点?
谢谢
SO网友:shea
短代码函数接受second parameter 其中包含短代码开始标记和结束标记之间的值:
function youtube( $atts, $value = \'http://\' ) {
extract( shortcode_atts( array(
"width" => \'620\',
"height" => \'350\',
"name"=> \'movie\',
"allowFullScreen" => \'true\',
"allowScriptAccess"=>\'always\',
), $atts ) );
return \'<object style="height: \'.$height.\'px; width: \'.$width.\'px"><param name="\'.$name.\'" value="\'.$value.\'"><param name="allowFullScreen" value="\'.$allowFullScreen.\'"></param><param name="allowScriptAccess" value="\'.$allowScriptAccess.\'"></param><embed src="\'.$value.\'" type="application/x-shockwave-flash" allowfullscreen="\'.$allowFullScreen.\'" allowScriptAccess="\'.$allowScriptAccess.\'" width="\'.$width.\'" height="\'.$height.\'"></embed></object>\';
}
add_shortcode( \'yt\', \'youtube\' );
SO网友:s_ha_dum
回调函数需要接受第二个参数。
function youtube($atts, $content) {
extract(shortcode_atts(array(
"value" => \'http://\',
"width" => \'620\',
"height" => \'350\',
"name"=> \'movie\',
"allowFullScreen" => \'true\',
"allowScriptAccess"=>\'always\',
), $atts));
return \'<object style="height: \'.$height.\'px; width: \'.$width.\'px"><param name="\'.$name.\'" value="\'.$content.\'"><param name="allowFullScreen" value="\'.$allowFullScreen.\'"></param><param name="allowScriptAccess" value="\'.$allowScriptAccess.\'"></param><embed src="\'.$content.\'" type="application/x-shockwave-flash" allowfullscreen="\'.$allowFullScreen.\'" allowScriptAccess="\'.$allowScriptAccess.\'" width="\'.$width.\'" height="\'.$height.\'"></embed></object>\';
}
我相信这是对的。
There are examples in the Codex about how to do this.