如何在ACF中继器内添加多个短代码?

问题描述 投票:0回答:1

我的代码是这样的:

<?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]等等。

wordpress advanced-custom-fields shortcode wordpress-shortcode acfpro
1个回答
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”]

© www.soinside.com 2019 - 2024. All rights reserved.