WordPress函数apply_shortcodes()解析和执行给定字符串中的短代码

WordPress函数apply_shortcodes()解析和执行给定字符串中的短代码

WordPress函数apply_shortcodes()解析和执行给定字符串中的短代码

  在 WordPress CMS内容管理系统中,apply_shortcodes()一个内置函数,apply_shortcodes()用于处理和显示内容中的短代码。短代码是代表特定功能或内容的小代码片段,使您可以轻松添加功能或嵌入元素,而无需编写大量代码。

  推荐:[最新版]Filter Everything插件下载WordPress通用过滤器插件

apply_shortcodes()函数基本语法

描述

  解析和执行给定字符串中的短代码

用法

echo apply_shortcodes('[myshrtcode]Hello World![/myshrtcode]');
  • $content (string):这是要在其中搜索短代码的内容。
  • $ignore_html (bool):可选。如果为 true,该函数将跳过 HTML 元素内的短代码。默认为 false。

  推荐:WordPress函数block_template_part()打印特定的块模板

apply_shortcodes()函数

  apply_shortcodes()在内容中搜索短代码并通过其挂钩过滤短代码(源文件可参考这里

function apply_shortcodes( $content, $ignore_html = false ) {
	return do_shortcode( $content, $ignore_html );
}

  推荐:WordPress函数email_exists()确定给定的电子邮件是否存在

如何使用apply_shortcodes()

  基本用法,使用apply_shortcodes()解析字符串中的短代码。

// Assuming 'myshrtcode' is a registered shortcode that wraps text in a <p> tag.
echo apply_shortcodes('[myshrtcode]Hello World![/myshrtcode]');
// Output: <p>Hello World!</p>

  忽略HTML,使用apply_shortcodes()并将$ignore_html参数设置为true。这将忽略HTML标签内的短代码。

// 'myshrtcode' adds a <p> tag around the content.
echo apply_shortcodes('<div>[myshrtcode]Hello World![/myshrtcode]</div>', true);
// Output: <div>[myshrtcode]Hello World![/myshrtcode]</div>

  使用带有属性的简码,使用apply_shortcodes()解析带有属性的短代码。

// Assuming 'myshrtcode' is a registered shortcode that wraps text in a <p> tag with a class.
echo apply_shortcodes('[myshrtcode class="highlight"]Hello World![/myshrtcode]');
// Output: <p class="highlight">Hello World!</p>

  组合短代码,使用apply_shortcodes()解析单个字符串中的多个短代码。

// 'myshrtcode' wraps text in a <p> tag, 'anothershrtcode' wraps text in a <strong> tag.
echo apply_shortcodes('[myshrtcode][anothershrtcode]Hello World![/anothershrtcode][/myshrtcode]');
// Output: <p><strong>Hello World!</strong></p>

  变量中的简码,将apply_shortcodes()与包含短代码的变量一起使用。

// 'myshrtcode' wraps text in a <p> tag.
$text = "[myshrtcode]Hello World![/myshrtcode]";
echo apply_shortcodes($text);
// Output: <p>Hello World!</p>

  推荐:怎么删除WordPress未使用的数据库表(150+插件主题数据库表名称)

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部