php 短代码中的短代码 - wordpress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4773186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:18:56  来源:igfitidea点击:

Shortcodes inside a shortcode - wordpress

phpwordpress

提问by Luis

I created a shortcode which display the employees, the HTML look somthing like that:

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

So I created 2 shortcodes:

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

[start_employee] - which contains <ul class="employee"> .. </ul>

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

[employee] - which contains content about the employee

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

And it should work like that:

它应该像这样工作:

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

but when I put it in the wordpress editor the html look like that:

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

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

I think I know why.. because the fuunction of start_employee contain:

我想我知道为什么.. 因为 start_employee 的功能包含:

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

What should I do that it read it as a shortcode?

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

Thank you.

谢谢你。

回答by Peter Rowell

Shortcodes do not automatically nest -- you have to call do_shortcode($content)yourself. See the caption_shortcode()example on http://codex.wordpress.org/Shortcode_API.

短代码不会自动嵌套 - 您必须调用do_shortcode($content)自己。请参阅http://codex.wordpress.org/Shortcode_APIcaption_shortcode()上的示例。

回答by Rakhitha Nimesh

You have to use shortcodes recursively to get the result.

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

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

回答by Angus Beefsteak

If you have a recursive function set up, as in Cart66, you can do something like this:

如果您设置了递归函数,如 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]');