1

In my Customizer, I have a checkbox to "Display Title". I want the title to display by default (which it is in the Customizer) but the Customizer settings need to be saved in order for it to display on the live site. I would like it to display without having to save the settings first.

This is the code I have in my template file:

<?php if( get_theme_mod( 'display_header_image_title' ) == '1') { ?>
    <h1 id="header-image-title"><?php echo get_theme_mod( 'header_image_title' , __( 'default text', 'myTheme' )); ?></h1>
<?php } ?>

This is the code I have in my customizer.php file:

// Title
$wp_customize->add_setting( 'header_image_title', array(
    'default'           => __('Title','myTheme'),
    'transport'         => 'postMessage'
) );    
$wp_customize->add_control( 'header_image_title', array(
    'label'             => __('Title','myTheme'),
    'type'              => 'text'
) );

// Display Title
$wp_customize->add_setting( 'display_header_image_title', array(
    'default'           => true,
    'transport'         => 'postMessage'
) );
$wp_customize->add_control( 'display_header_image_title', array(
    'label'             => __('Display Title','myTheme'),
    'type'              => 'checkbox'
) );

I suspect this line needs to be edited in the template file:

<?php if( get_theme_mod( 'display_header_image_title' ) == '1') { ?>

2 Answers 2

3

get_theme_mod() has a second parameter where you can set the default value, the value to use if the option is not set.

So, if the default value is "1", you can use:

get_theme_mod( 'display_header_image_title', '1' )

Then, if there is not value for display_header_image_title (no value in database), "1" is used. So, you could check the exact value:

if( get_theme_mod( 'display_header_image_title', '1' ) === '1' ) {
}

Or just true/false:

if( get_theme_mod( 'display_header_image_title', '1' ) ) {
}
6
  • Thanks @cybmeta. I tried that but doesn't work, probably because it's not completing the if statement. I also tried <?php if( get_theme_mod( 'display_header_image_title' , '1' ) == '1') { ?> with no success. Commented Nov 25, 2016 at 11:27
  • Actually that does work. I forgot that the image title was inside another similar if statement! Commented Nov 25, 2016 at 11:34
  • Glad to help. Typo fixed and answer updated.
    – cybmeta
    Commented Nov 25, 2016 at 12:03
  • Whats the difference between == and === ? Commented Nov 25, 2016 at 14:37
  • 1
    Basically, == checks value after data type manipulation; === checks value without data type manipulation. == is "equal to" and === is "identically equal to". For example, if( true == 1 ) verifies because 1 (integer data type) is interpreted as true; but if( true === 1 ) doesn't verify because true is boolean data type and 1 is integer data type. See PHP comparison operators.
    – cybmeta
    Commented Nov 25, 2016 at 15:25
0

Adding a default value fixed it, with the help from @cybmeta. Thanks!

<?php if( get_theme_mod( 'display_header_image_title' , '1' ) == '1') { ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.