当我尝试使用简码保存页面时,WordPress编辑器会中断

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

我需要帮助。我不明白发生了什么。我创建了一个简码。他在那儿

 function mainslider_function() {
    function lesson_slider() {

        $open_div = '<div class="autoplay">';
        $close_div = '</div>';

        if( have_rows('test_fields', 'option') ):
            echo $open_div;
            while ( have_rows('test_fields', 'option') ) : the_row();
                $sub_value = get_sub_field('image');
                echo '<div class="slide"><img src="'.$sub_value.'"></div>';
            endwhile;
            echo $close_div;
        endif;
    }
return lesson_slider();
}

并且有效。但是只在最前面。尝试使用此简码编辑页面时。 WordPress编辑器停止工作。我了解问题是我在函数中使用了函数。因为当我测试此代码时:

 function mainslider_function() {
    $test = 'test message';

    return $test;
}

一切正常。如果我喜欢这样

function mainslider_function() {
    function test(){
        $test = 'test message';
        echo test;
    }
    return test();
}

编辑器停止工作。告诉我,为什么会这样?

php wordpress shortcode
1个回答
0
投票

您不需要内部函数。您可以连接字符串部分或使用output buffering

使用后者,它将变成:

function mainslider_function()
{
  ob_start();

  if (have_rows('test_fields', 'option')) {
    echo '<div class="autoplay">';
    while (have_rows('test_fields', 'option')) {
      the_row();
      $sub_value = get_sub_field('image');
      echo '<div class="slide"><img src="', $sub_value, '"></div>';
    }
    echo '</div>';
  }

  return ob_get_clean();
}

注意:在此过程中也做了一些代码清除。

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