短代码中的短代码 - 字压

2022-08-30 16:49:26

我创建了一个显示员工的短代码,HTML看起来像这样:

<ul class="employees">

<li><img src=""> <h5>name</h5> <p>description</p></li>
<li><img src=""> <h5>name</h5> <p>description</p></li>
..

</ul>

所以我创建了2个短代码:

[start_employee] - 其中含有<ul class="employee"> .. </ul>

[员工] - 包含有关员工的内容

它应该像这样工作:

[start_employee]
[employee img=".." name=".." description=".."] 
[employee img=".." name=".." description=".."] 
[/start_employee]

但是当我把它放在wordpress编辑器中时,html看起来像这样:

<ul class="employee">
[employee img=".." name=".." description=".."] 
[employee img=".." name=".." description=".."]
</ul>

我想我知道为什么..因为start_employee的赋值包含:

return '<ul class="employee">'.$content.'</ul>';

我该怎么做才能将其作为短代码读取?

谢谢。


答案 1

短代码不会自动嵌套 - 你必须打电话给自己。请参阅 http://codex.wordpress.org/Shortcode_API 上的示例。do_shortcode($content)caption_shortcode()


答案 2

您必须以递归方式使用短代码才能获得结果。

function start_employee($attr,$content){
          return '<ul class="employee">'.do_shortcode($content).'</ul>';
}
add_shortcode("start_employee","start_employee");

function employee($attr,$content){
          return '<li><img src=""> <h5>name</h5>; <p>description</p></li>';
}
add_shortcode("employee","employee");

推荐