The debugger tells me that get_theme_mod() is returning ‘false’, when
I expect it to return the value of the selected item.
get_theme_mod()
isn’t working because when you registered the setting, you set the type
to option
:
$wp_customize->add_setting('title_font', array(
'default' => 'Roboto Slab',
'capability' => 'edit_theme_options',
'type' => 'option',
'transport' => 'postMessage'
));
The possible values for type
when adding a setting are 'option'
, or the default, 'theme_mod'
.
option
stores the value independently of the current theme, and is retrieved with get_option()
, while theme_mod
stores the value only for the current theme, and is retrieved with get_theme_mod()
.
Using get_option() gives me the key of the option, “value3” and not the value.
The ‘key’ is the value. It’s what’s output into the value=""
attribute of the <option>
tag.
If you want to use the label, there’s two potential solutions.
First, you could just pass the labels only, then these will also be used as the values:
array(
'Roboto Slab',
'Times New Roman',
'American Typewriter',
),
Or you could store a reference to the labels independently of both places:
function wpse_341193_get_font_options() {
return array(
'value1' => 'Roboto Slab',
'value2' => 'Times New Roman',
'value3' => 'American Typewriter',
);
}
Which you can then use as the choices:
'choices' => wpse_341193_get_font_options(),
And the output:
<?php
$font_options = wpse_341193_get_font_options();
$font_family = get_theme_mod('title_font');// false
?>font-family: <?php echo $font_options[$font_family] ?>;