devinsays / customizer-library-demo

Demo for the Customizer Library project.
54 stars 15 forks source link

Output different body_classes based on radio choice #2

Closed georgegrigorita closed 9 years ago

georgegrigorita commented 9 years ago

I'm trying to add a body class via the Layout section (type: radio) in my customizer. I think I'm missing something because the body-class is always no-sidebar, which is not correct. It's like the get_theme_mod can't access the $choices array... Any help with this will be much appreciated.

In customizer-options:

$section = 'layout';
$sections[] = array(
    'id' => $section,
    'title' => __( 'Layout', 'sylph' ),
    'priority' => '30'
);

$choices = array(
    'sidebar-right' => 'Right Sidebar',
    'sidebar-left' => 'Left Sidebar',
    'no-sidebar' => 'No Sidebar'
);

$options['layout-section'] = array(
    'id' => 'layout-section',
    'label'   => __( 'Layout', 'sylph' ),
    'section' => $section,
    'type'    => 'radio',
    'choices' => $choices,
    'default' => 'sidebar-right',
);`

In functions:

function test_body_classes( $classes ) {

$layout = get_theme_mod( 'layout' );

if ($layout == 'sidebar-left') {
    $classes[] = 'sidebar-left';
} 

else if ($layout == 'sidebar-right') {
     $classes[] = 'sidebar-right';
}

else {
    $classes[] = 'no-sidebar';
}
return $classes;
}
add_filter( 'body_class', 'test_body_classes' );
RescueThemes commented 9 years ago

Do you have another array using the variable $choices?

georgegrigorita commented 9 years ago

Yes, there are 2 more (left from the customizer-library examples). I've commented their code but I get the same results.

RescueThemes commented 9 years ago

Instead of commenting out the code, try first to give your variable a distinct name (like $sidebar_choices). If that still doesn't work, I wonder if it'll work with a dropdown/select instead of radio:

$sidebar_choices = array(
    'sidebar-right' => 'Right Sidebar',
    'sidebar-left' => 'Left Sidebar',
    'no-sidebar' => 'No Sidebar'
);

$options['layout-section'] = array(
    'id' => 'layout-section',
    'label'   => __( 'Layout', 'sylph' ),
    'section' => $section,
    'type'    => 'select',
    'choices' => $sidebar_choices,
    'default' => 'sidebar-right',
);`
georgegrigorita commented 9 years ago

I tried both cases (different variable, different type), but I'm still getting the same body-class (no-sidebar) instead of the default one (sidebar-right).

devinsays commented 9 years ago

It looks like you're using "layout-section" as the theme mod (not "layout", that is the name of the section in your example).

To debug something like this, first make sure that the theme mod is getting set appropriately:

echo get_theme_mod( 'layout-section', customizer_library_get_default( 'layout-section' ) );

Code should look something like this:

function test_body_classes( $classes ) {
     $classes[] = get_theme_mod( 'layout-section', customizer_library_get_default( 'layout-section' ) );
     return $classes;
}
add_filter( 'body_class', 'test_body_classes' );