使用嵌套的递归短代码

时间:2013-08-07 13:08:41

标签: wordpress shortcode

我有这两个功能:

add_shortcode('section_block_container','dos_section_block_container');

function dos_section_block_container($atts, $content = null) {
    $content = do_shortcode($content);
    echo '<ul class="list-unstyled list-inline">' . $content . '</ul>';

}

add_shortcode('section_block','dos_section_blocks');

function dos_section_blocks($atts, $content = null) {

    // define attributes and their defaults
    extract( shortcode_atts( array (
        'first' => FALSE,
        'color' => '',
        'icon'  => '',
        'title' => '',
    ), $atts ) );

?>
    <li>
        <a href="" style="background-color: <?php echo $color; ?>" title="<?php echo $title; ?>" class="section-block show-grid col-12 col-sm-3 <?php // echo ($first == TRUE ? 'col-offset-3 ' : '') ?>col-lg-3">
            <h4><?php echo strip_tags ($title); ?></h4>
            <?php echo strip_tags($content); ?>
            <?php echo $icon; ?>
        </a>
    </li>

<?php

}

并在wp编辑器中使用递归[section_block]

  

[section_block_container]

     

[section_block color =“#001e61”title =“Lorem   Ipsum“icon =”“first =”true“] [/ section_block]

     

[section_block color =“#001e61”title =“Lorem Ipsum”icon =“”first =“true”] [/ section_block]

     

[/ section_block_container]

问题是列表没有出现在容器内部,但在do_shortcode();

之外

2 个答案:

答案 0 :(得分:1)

短代码不能echo 它必须 return

请参阅Shortcode_APIdo_shortcode文档。

答案 1 :(得分:0)

function container($atts, $content = null) {
    $content = do_shortcode($content);
    return "<ul class='list-unstyled list-inline'>" . $content . "</ul>";
}

add_shortcode('section_block_containe','container');

function section_block_function($atts, $content = null) {

    // define attributes and their defaults
    extract( shortcode_atts( array (
        "first" => FALSE,
        "color" => '',
        "icon"  => '',
        "title" => '',
    ), $atts ) );

    $class = ($first)? 'col-offset-3 ':'';

    $li = 
      "<li>
         <a href='' style='background-color: ".$color."' title='".$title."' class='section-block show-grid col-12 col-sm-3 ".$class." col-lg-3'>
         <h4>".$title."</h4>
         ".$content."
         ".$icon."
         </a>
      </li>";
    return $li;
}

add_shortcode('section_block','section_block_function');

朋友他唯一的错误是动作短代码与返回

一起使用