一,shortcode 简介
使用 shortcode 可以增加文章的功能,实现输入密码显示全部文章内容,付款后显示全部内容。
[shortcode]Some Content[/shortcode] // 封闭标签
[shortcode] // 自闭标签
[shortcode title="example"] // 带有一个参数的自闭标签
[shortcode]<p><a href="#"><span>内容</span></a></p>[/shortcode] // 标签内可以填写文本或 HTML
二,shortcode 使用例子
在使用 shortcode 前,首先必须在主题的 functions.php 文件中定义 shortcode ,例如:
/**
* @param $atts shortcode 的各个参数
* @param $content 标签内的内容
* @return html string
*/
function test_shortcode($atts, $content = null){
// 使用 extract 函数解析标签内的参数
extract(shortcode_atts(array( "title" => '标题' ), $atts));
return '<div class="myshortcode">
<h3>'. $title .'</h3>
<p>
'. $content .'
</p>
</div>';
}
add_shortcode("test", "test_shortcode");
添加以上代码到 functions.php,经典编辑器可以直接输入 [test][/test]标签调用该 shortcode,如:
[test title="欢迎"]这里填入隐藏内容,回复后可见[/test]
如果使用的是古腾堡编辑器,可以新建小工具中的简码区块。
声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。