定制器实时预览布局选取器

时间:2016-06-21 作者:Mark

我正在尝试使用wordpress自定义程序创建布局选择器选项。到目前为止,除了现场预览,我所做的一切都很有效。当我在定制器中更改一个选项(例如左侧边栏)时,css应更改为在左侧显示边栏,但是当更改一个选项时,边栏将消失,并由与所选选项的顺序相对应的数字替换。有人能指出我哪里做错了吗。

这里是php。

function embee_customize_register( $wp_customize ) {
//All our sections, settings, and controls will be added here

//Add Layout Options section
$wp_customize->add_section(\'embee_layout_section_options\', array(
    \'title\'    => __(\'Layout Options\', \'mybasictheme\'),
    \'description\' => \'Change various site section layouts.\',
    \'priority\' => 1,
));

$wp_customize->add_setting(\'embee_layout_section_options[site_layout]\', array(
    \'default\'        => \'3\',
    \'capability\'     => \'edit_theme_options\',
    \'transport\'      => \'postMessage\',
    \'type\'           => \'option\',
));

$wp_customize->add_control( \'select_box\', array(
    \'settings\' => \'embee_layout_section_options[site_layout]\',
    \'label\'   => \'Site Layout\',
    \'section\' => \'embee_layout_section_options\',
    \'type\'    => \'select\',
    \'choices\'    => array(
        \'1\' => \'Left Sidebar\',
        \'2\' => \'Right Sidebar\',
        \'3\' => \'Three Column\',
        \'4\' => \'Full Width\',
    ),
    \'description\' => \'Change sidebar layouts.\',
));
}
add_action( \'customize_register\', \'embee_customize_register\' );

function embee_customizer_live_preview()
{
wp_enqueue_script( 
      \'embee-customizer\',           //Give the script an ID
      get_template_directory_uri().\'/mbpanel/js/customizer.js\',//Point to file
      array( \'jquery\',\'customize-preview\' ),    //Define dependencies
      \'\',                       //Define a version (optional) 
      true              //Put script in footer?
);
}
add_action( \'customize_preview_init\', \'embee_customizer_live_preview\' );

function layout_customize_css()
{
$options = get_option( \'embee_layout_section_options\' );
?>
     <style type="text/css">
        #content { <?php if( $options[\'site_layout\'] == \'1\' ) { ?> float: right; <?php } ?> } #sidebar_primary { <?php if( $options[\'site_layout\'] == \'1\' ) { ?> float: left; <?php } ?> }
        #content { <?php if( $options[\'site_layout\'] == \'2\' ) { ?> float: left; <?php } ?> } #sidebar_primary { <?php if( $options[\'site_layout\'] == \'2\' ) { ?> float: right; <?php } ?> }
        #content { <?php if( $options[\'site_layout\'] == \'3\' ) { ?> float: left; <?php } ?> } #sidebar_primary { <?php if( $options[\'site_layout\'] == \'3\' ) { ?> float: left; <?php } ?> } #sidebar_secondary { <?php if( $options[\'site_layout\'] == \'3\' ) { ?> float: left; <?php } ?> }
        #content { <?php if( $options[\'site_layout\'] == \'4\' ) { ?> float: none; width: 100%; <?php } ?> }
     </style>
<?php
}
add_action( \'wp_head\', \'layout_customize_css\');
下面是javascript

( function( $ ) {
wp.customize( \'embee_layout_section_options\', function( value ) {
    value.bind( function( to ) {
        $( \'#sidebar_primary\' ).css( \'float\', \'left\' );
    } );
} );

} )( jQuery );

enter image description here

1 个回复
SO网友:Gareth Gillman

您不需要javascript,只需使用get\\u theme\\u mod变量更改类即可。

<?php
$layout = get_theme_mod(\'embee_layout_section_options[site_layout]\', \'3\');
$class = \'\';
if($layout == \'1\') {
 $class = \'this\';
} elseif($layout == \'2\') {
 $class = \'that\';
}
?>
<div class="<?php echo $class; ?>">
 // stuff
</div>
将其放入模板文件中,例如页面。php和live customizer将获取它

然后创建类,例如。

.this {
 float:left;
}
.that {
 float:right;
}
我在customizer中构建了一些庞大而复杂的东西,从来没有添加过javascript,这一切都是通过模板文件完成的,并获得了\\u theme\\u mod