我正在开发一个wp主题,我想有一个可以在一篇文章中多次使用的短代码。
在函数中。php我有:
function twoColPostcardfn($atts, $content){
extract(shortcode_atts(array(
\'image\'=>\'\',
\'text\'=>\'\',
\'title\'=>\'\',
\'boxlink\'=>\'\',
\'float\'=>\'\'
), $atts));
$bob=\'<span class="twoColPostcard ">\'.$content.\'</span>\';
return $bob;
}
add_shortcode( \'twoColPostcard\', \'twoColPostcardfn\' );
在这篇文章中,我有:
<p>[twoColPostcard]</p>
<p>hello from the first postcard</p>
<p>[/twoColPostcard]</p>
<p>[twoColPostcard]</p>
<p>hello from the second postcard</p>
<p>[/twoColPostcard]</p>
我希望我的输出是
<span class="twoColPostcard"><p>Hello from ...</p></span>
<span class="twoColPostcard"><p>Hello from ...</p></span>
但我得到的是:
<span class="twoColPostcard">
<p></p>
<p>hello from the first postcard</p>
<p><span class="twoColPostcard "></span></p>
<p>hello from the second postcard</p>
<p></p>
</span>
换句话说,第二个短码在第一个短码完成之前就开始了,所以最后是嵌套的(第二个是空的)
我用了codex 我不知道为什么,但它不起作用。我已尝试将其插入到我的函数中as per 无济于事
remove_filter( \'the_content\', \'wpautop\' );
add_filter( \'the_content\', \'wpautop\' , 12);
最合适的回答,由SO网友:David Lee 整理而成
像这样首先放置要调用的移除筛选器,更改span
到div
或者您会遇到一些浏览器问题:
remove_filter( \'the_content\', \'wpautop\' );
add_filter( \'the_content\', \'wpautop\' , 12);
function twoColPostcardfn($atts, $content){
extract(shortcode_atts(array(
\'image\'=>\'\',
\'text\'=>\'\',
\'title\'=>\'\',
\'boxlink\'=>\'\',
\'float\'=>\'\'
), $atts));
$bob=\'<div class="twoColPostcard ">\'.$content.\'</div>\';
return $bob;
}
add_shortcode( \'twoColPostcard\', \'twoColPostcardfn\' );
或将内容更改为其他内容
span
, 这个
p
标记是块标记
span
标记是内联标记,块标记不能位于内联标记内。
[twoColPostcard]<span>hello from the first postcard</span>[/twoColPostcard]
[twoColPostcard]<span>hello from the second postcard</span>[/twoColPostcard]
SO网友:Samyer
为什么要使用p标记?不要输入任何新行,也不会添加p标记。只需在线添加即可:
[twoColPostcard]hello from the first postcard[/twoColPostcard[twoColPostcard]hello from the second postcard[/twoColPostcard]
然后更改您的快捷码以包含p标记:
function twoColPostcardfn($atts, $content){
extract(shortcode_atts(array(
\'image\'=>\'\',
\'text\'=>\'\',
\'title\'=>\'\',
\'boxlink\'=>\'\',
\'float\'=>\'\'
), $atts));
$bob=\'<span class="twoColPostcard "><p>\'.$content.\'</p></span>\';
return $bob;
}
add_shortcode( \'twoColPostcard\', \'twoColPostcardfn\' );