我创建了一个显示员工的短代码,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>';
我应该怎么做才能将其作为短代码阅读?
谢谢。
答案 0 :(得分:48)
短代码不会自动嵌套 - 您必须自己调用do_shortcode($content)
。请参阅http://codex.wordpress.org/Shortcode_API上的caption_shortcode()
示例。
答案 1 :(得分:11)
您必须递归使用短代码才能获得结果。
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");
答案 2 :(得分:5)
如果你设置了一个递归函数,就像在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]');