ShortCode是WordPress扩展其编辑器的快捷方式,能快速的实现一些常用的插入功能。
比如,你要经常在blog里贴程序代码,SyntaxHighlighter这个插件就提供了插入代码的ShortCode。
自己写一些常用的ShortCode也很简单,有人以Google Map为例,做了个示例。
在模板functions.php文件里加入下列php代码:
[php]
//Google Maps Shortcode
function fn_googleMaps($atts, $content = null) {
extract(shortcode_atts(array(
"width" => ‘640’,
"height" => ‘480’,
"src" => ”
), $atts));
return ‘<iframe width="’.$width.’" height="’.$height.’" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="’.$src. ‘&output=embed" ></iframe>’;
}
add_shortcode("google map", "fn_googleMaps");
[/php]
然后在编辑框里输入[googlemap width=”600″ height=”400″ src=”url”]
就能显示Google Map了。这里的url,就是你在Google Map网站上看地图时右上角的那个Link对话框了的Link。
下面是上海万体馆附近的地图。
[googlemap width=”600″ height=”400″ src=”http://maps.google.com/maps?ie=UTF8&hq=&hnear=Shanghai,+China&ll=31.18237,121.432829&spn=0.015016,0.017617&t=h&z=16″]
你也可以在地铁上加上自己的数据,这是上海的一些KFC店。
[googlemap width=”600″ height=”400″ src=”http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=116975913023088085964.00047db7d38369561466d&t=h&z=12″]
当然你也可以直接用Google Map自己提供的iframe代码了,只是稍微长了点。
[code language=”xml”]
<iframe width="600" height="400" frameborder="0"
scrolling="no" marginheight="0" marginwidth="0"
src="http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=116975913023088085964.00047db7d38369561466d&ll=31.236635,121.465941&spn=0.104698,0.152156&t=h&output=embed">
</iframe>
<br /><small>View
<a href="http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=116975913023088085964.00047db7d38369561466d&ll=31.236635,121.465941&spn=0.104698,0.152156&t=h&source=embed" style="color:#0000FF;text-align:left">
上海 KFC店</a> in a larger map</small>
[/code]