dongsi2317 2019-05-27 16:16
浏览 69
已采纳

如何在Wordpress Customizer实时预览中停止部分刷新以删除段落中断?

I have added my own settings to the Wordpress customizer and set it up so that it live previews using the "postMessage" method. It is working almost perfectly apart from when I edit a field linked to paragraph text, the preview doesn't show line breaks in the paragraphs. However this is temporary, once the customizer is closed or the page is refreshed the paragraph gaps come back.

I am defining the Customizer sections in the customizer.php using the following code:

// About Section Text
$wp_customize->add_setting( 'about__text' , array(
    'default'   => 'Lorem ipsum dolor sit amet consectetur, adipisicing elit. Blanditiis, odit unde magnam dolores quasi voluptas, impedit a nam inventore atque eaque nobis possimus officiis deleniti quisquam animi deserunt ad ipsa sapiente illo?',
    'transport' => 'postMessage',
) );

// About Section Text (CONTROL)
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'about__text', array(
    'label'      => __( 'About Section Content:', 'mytheme' ),
    'section'    => 'edit__homepage--section',
    'settings'   => 'about__text',
    'priority'   => 3,
    'type'       => 'textarea'
) ) );

I am displaying the above theme mod in my index.php file using the following code:

    <div class="about__text">
      <?php echo wpautop(get_theme_mod('about__text')); ?>
    </div>

Here's my jquery:

( function( $ ) {
    wp.customize( 'about__text', function( value ) {
        value.bind( function( newVal ) {
            $( '.about__text' ).html( newVal );
        } );
    } );
} )( jQuery );

I've tried experimenting with different jquery objects such as text() and contents() but these either have the same issue or don't work at all.

Doe anyone know if there's a way I can get the javascript preview to honour the paragraph styles? Maybe like a javascript version of wpautop() ?

  • 写回答

1条回答 默认 最新

  • donoworuq450547191 2019-05-30 04:52
    关注

    You can use selective refresh like this. Values are rendered server side so no need to extra JS. And since only selected wrapper element is refresh, it is more efficient than full page reload in Customizer. In the following example content only in .about__text is refreshed. Since rendering is done server side, no need to look for JS alternative of wpautop function. I believe this would be one alternative for your problem mentioned in the question.

    function wpso_customize_register( $wp_customize ) {
        $wp_customize->selective_refresh->add_partial(
            'about__text',
            array(
                'selector'        => '.about__text',
                'render_callback' => function(){
                    echo wp_kses_post( wpautop( get_theme_mod('about__text') ) );
                },
            )
        );
    }
    add_action( 'customize_register', 'wpso_customize_register' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题