在WordPress主题中使用自定义Shortcode

WordPress 的 Shortcode 常用的主要有3种:
[shortcode foo=”bar”]
[shortcode]text[/shortcode]
[shortcode foo=”bar”]text[/shortcode]

这里简单介绍第一种的定制方法,编辑以下代码到functions.php:

// [redirect url="?page_id=100"]
function sc_redirect( $atts ){
extract( shortcode_atts( array(
'url' => 'http://mimiz.cn',
), $atts ) );

$result = ‘window.location.href=”‘.home_url(‘/’).$url.'”;’;

return $result;
}
add_shortcode( ‘redirect’, ‘sc_redirect’ );

以上代码作用是创建一个重定向的shortcode。

需要进一步了解shortcode请移步官方文档:
http://codex.wordpress.org/Shortcode_API

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s