城市简码_如何在WordPress中创建嵌套的简码

城市简码

When an editor adds a WordPress [shortcode] to a post or page, it’s replaced by the returned output of a handler function within a plug-in or the theme’s functions.php file. Let’s create a simple example:

当编辑者将WordPress [shortcode]添加到帖子或页面时,将由插件或主题的functions.php文件中的处理函数的返回输出替换。 让我们创建一个简单的示例:

// create a styled button
function ContactButton($params, $content = null) {

	extract(shortcode_atts(array(
		'url' => '/contact-us',
		'type' => 'style1'
	), $params));

	return
		'<a href="' . $url . '" class="button ' . $type. '">' . ucwords($content) . '</a>';

}
add_shortcode('button','ContactButton');

When the following code is encountered in page or post:

在页面或帖子中遇到以下代码时:

[button]contact us today[/button]

it’ll be translated to:

它将被翻译为:

<a href="/contact-us" class="button style1">Contact Us Today</a>

The editor’s job is made far easier and they don’t need to worry about learning HTML. Let’s look at another example which creates a simple callout box:

编辑器的工作变得更加轻松,他们无需担心学习HTML。 让我们看另一个创建一个简单的标注框的示例:

// callout box
function CalloutBox($params, $content = null) {

	extract(shortcode_atts(array(
		'type' => 'style1'
	), $params));
	
	return
		'<aside class="callout ' . $type . '">' . $content . '</aside>';

}
add_shortcode('callout','CalloutBox');

But what if our editor wants to insert a button inside their callout box?…

但是,如果我们的编辑想要在其标注框中插入一个按钮怎么办?…

[callout]For more information [button]contact us today[/button][/callout]

As it stands, the current code will fail. The CalloutBox function is called first but the inner [button] will not be translated accordingly.

就目前而言,当前代码将失败。 首先调用CalloutBox函数,但不会相应地转换内部[button]。

The key function for fixing the problem is do_shortcode() — it applies WordPress’s shortcode filter to any content. In this case, we want to allow the editor to add a [button] within our [callout] so we’d change modify the return statement of CalloutBox accordingly:

解决该问题的关键功能是do_shortcode() -它将WordPress的短代码过滤器应用于任何内容。 在这种情况下,我们希望允许编辑器在[callout]中添加[button],因此我们将相应地更改修改CalloutBox的return语句:

return
	'<aside class="callout ' . $type . '">' . 
	do_shortcode($content) . 
	'</aside>';

The nested code above will now work as expected. However, the editor wouldn’t be permitted to nest a [callout] inside a [button]. It’s flexibility such as this which makes WordPress a joy to use — version 3.3 is available now.

上面的嵌套代码现在将按预期工作。 但是,不允许编辑者在[按钮]内嵌套[标注]。 诸如此类的灵活性使WordPress成为一种令人愉悦的使用方式- 版本3.3现在可用

翻译自: https://www.sitepoint.com/wordpress-nested-shortcodes/

城市简码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值