Wordpress嵌套短代码内容

时间:2017-04-13 12:24:55

标签: wordpress shortcode

我想使用以下短代码:

[section]
<h1>Section body content</h1>
[section_footer]<p>Section footer content</p>[/section_footer]
[/section]

所以,我添加了以下短代码回调:

function sec_cb ($att, $content = null) {
// ...
return 
'
<section>
<div class="sec-body">
'.strip_shortcodes($content).'
</div>
'.do_shortcode($content).'
</section>
';
}

function sec_footer_cb ($atts, $content = null) {
return 
'
<div class="sec-footer">
'.$content.'
</div>
';
}
add_shortcode('section', 'sec_cb');
add_shortcode('section_footer', 'sec_footer_cb');

但输出是:

<section>
 <div class="sec-body">
  <h1>Section body content</h1>
 </div>
 <h1>Section body content</h1>
 <div class="sec-footer">
  <p>Section footer content</p>
 </div>
</section>

如何修复它并在页脚中仅打印页脚短代码内容?

1 个答案:

答案 0 :(得分:0)

请尝试添加ob_start()ob_get_contents()以及ob_end_clean(),我会调整您的代码。试着告诉我它是否有效。

function sec_cb ($att, $content = null) {
    // ...
    ob_start();
    echo '<section>
            <div class="sec-body">
               '.strip_shortcodes($content).'
            </div>
            '.do_shortcode($content).'
          </section>';
    $eval_result = ob_get_contents();
    ob_end_clean();
    return $eval_result;
}

function sec_footer_cb ($atts, $content = null) { 
    ob_start();
    echo '<div class="sec-footer">
            '.$content.'
        </div>';
    $eval_result = ob_get_contents();
    ob_end_clean();
    return $eval_result;
}

add_shortcode('section', 'sec_cb');
add_shortcode('section_footer', 'sec_footer_cb');