城市简码

tech2023-02-11  101

城市简码

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/

城市简码

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)