WordPress的定制器预览不刷新functions.php

时间:2019-05-09 16:53:21

标签: php wordpress

因此,我遇到了一个问题,即定制器预览无法完全刷新。只有当我手动刷新页面时,我才能看到我的更改。我的一些代码可在下面帮助解释。

对于我的定制器设置,我的代码看起来像这样

$wp_customize->add_section( 'theme_layout', array(
        'title' => __( 'Layout', 'theme' ),
        'priority' => 30
) );

$wp_customize->add_setting( 'theme_header_layout', array(
         'default' => 'default',
         'transport' => 'refresh',
) );

$wp_customize->add_control( new WP_Customize_Control( $wp_customize,
'theme_header_layout', array(
          'label' => __( 'Header', 'theme' ),
          'section' => 'theme_layout',
          'settings' => 'theme_header_layout',
          'type' => 'select',
          'choices' => array(
               'default' => 'default',
               'special_header' => 'Special Header',
           )
) ) );

现在在我的functions.php中,我有这样的代码

//this is the code that doesn't seem to execute when the customizer refreshes
if ( 'special_header' == get_theme_mod( 'theme_header_display' ) ):
   function theme_special_header( $items ) {
       $items .= do_shortcode('[special_header_shortcode]');//This shortcode exists I just didnt bother mentioning it here
   }
   add_action( 'wp_nav_menu_secondary-menu_items', 'theme_special_header' );//Adds shortcode to menu with id of secondary-menu
endif;

当我转到定制器并选择“特殊标题”时,所有这些都很好接受,定制器会刷新,直到完全刷新页面后我才能看到更改。

1 个答案:

答案 0 :(得分:0)

我之前也遇到过类似的问题。我没有将条件添加到外部,而是将其保留在函数中并且可以正常工作。您可以为代码尝试类似的方法,这可能会有所帮助。

以下并不是您所提问题的确切答案,但可能有助于解决您的问题。

function wpso_customize_menu($items, $args) {
    if ( 'special_header' == get_theme_mod( 'theme_header_layout' ) ) {
        if( $args->theme_location == 'menu-1' ) {
            $items .= '<li><a href="https://example.com">Custom Link</a></li>';
        }
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'wpso_customize_menu', 10, 2);