-- 创造无限可能

wordpress:Shortcode(简码)介绍及使用

2022-07-17 03:45:33
573 人浏览 5 人点赞
有用,点赞支持一下

Shortcode是什么

WordPress Shortcode 指的是一些使用[]包含的短代码,WordPress会识别这些短代码并根据短代码的定义输出特定的内容。

Shortcode定义类型

Shortcode API 支持几乎所有可能的组合形式:自关闭标签,开放标签,含有参数的标签等。

[mycode]
[mycode foo="bar" ]
[mycode]Some Content(支持html)[/mycode] //包含内容
[mycode]Content [another-shotcode] more content[/mycode] //嵌套

Shortcode定义以及相关用法

function my_shortcode_func($attr, $content) {
    // $attr $key=>$value 的数组
    // $content 是 shortcode 中包含的字符串
    // 对 $attr 和 $content 进行处理
    // 返回预期的值
}
add_shortcode('mycode', 'my_shortcode_func')// 添加一个 Shortcode
remove_shortcode('mycode'); // 移除一个 Shortcode
remove_all_shortcodes(); // 移除所有的 Shortcode