我的代码是这样的:
<?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]等。
答案 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”]