如何在ACF转发器内添加多个短代码?

时间:2019-03-21 23:48:54

标签: wordpress advanced-custom-fields shortcode wordpress-shortcode acfpro

我的代码是这样的:

<?php

function get_toggle_title() {
    return get_sub_field('toggle_title');
};

if( have_rows('toggle_titles') ):
    $i = 1;
    while( have_rows('toggle_titles') ): the_row();


        add_shortcode('toggles-title-'.$i, 'get_toggle_title');

        $i++;

    endwhile;
endif;

?>

....但是它似乎不起作用。对于每个单独的简码重复相同的内容:[toggles-title-1],[toggles-title-2]等。

1 个答案:

答案 0 :(得分:0)

我有解决办法。这是PHP代码:

/** Create toggles Shortcodes */
function do_toggles_shortcode($atts) {

    $atts = shortcode_atts( array(
        'number' => 1
    ), $atts );

    ob_start();

    if( function_exists('get_field') ):
        if( have_rows('toggle_blocks') ):
            $i = 1;
            while( have_rows('toggle_blocks') ): the_row();

                if( $atts['number'] == $i ) {

                    if( have_rows('toggles') ):
                        ?>
                        <div class="toggles">
                            <?php
                            while ( have_rows('toggles') ) : the_row();
                                ?>
                               <div class="toggle_container">
                                      <div class="toggle_title">
                                        <?php the_sub_field('toggle_title'); ?>
                                        <i class="fas fa-chevron-right"></i>
                                      </div>
                                      <div class="toggle_content">
                                        <?php the_sub_field('toggle_content'); ?>
                                      </div>
                                   </div>
                              <?php
                            endwhile;
                            ?>
                        </div>
                        <?php
                    endif;

                }

                $i++;

            endwhile;
        endif;
    endif;

    return ob_get_clean();
}
add_shortcode('toggles-shortcode', 'do_toggles_shortcode');

这是简码的样子:

[toggles-shortcode number =“ 1”]

[toggles-shortcode number =“ 2”]