短代码在一个短代码中 - WordPress

27

我创建了一个短代码,用于显示员工信息,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>

[employee] - 它包含员工相关的内容

这应该像这样工作:

[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
你能否发布短代码函数的完整代码?这样更容易帮助你,也可以更好地帮助其他寻找类似解决方案的人。 - AJJ
3个回答

62

20

你需要递归使用短代码来获得结果。

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");

6
如果您已经设置了一个递归函数,如在Cart66中,您可以像这样做:
echo do_shortcode('[hide_from level="membership" ]<strong>You may only order this item if you are a member. Become a <a href="http://yoursite.com/beta/member-log-in/">member</a>.</strong>[/hide_from]');
echo do_shortcode('[show_to level="membership"]'.do_shortcode('[add_to_cart item="'.$prefix.get_post_meta($post->ID,'_et_cart66_product_id',true).'" ]').'[/show_to]');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接