1

Good afternoon.

I'm using the theme customizer to allow the user to select which pages to show the excerpt of on the front page. I've done this by using an array in the setting name:

$wp_customize->add_setting( 'learn_more[0]' , array(
    'default'     => 1
) );
$wp_customize->add_setting( 'learn_more[1]' , array(
    'default'     => 1
) );
$wp_customize->add_setting( 'learn_more[2]' , array(
    'default'     => 1
) );

I then create a control for each.

In my front-page.php template I then use get_theme_mod('learn__more') to provide an array of pages to search for in a custom loop.

It works perfectly. Except...

I've reduced the number of items I want from 4 to 3. But, the 4th item is now stored in the database so each time I use the get_theme_mod it returns an array of 4 items. As a result, the loop in my front-page.php shows 4 items.

Is there a way to solve this? How can I delete the 4th item in the database? I can see a future position where I will want to adjust the number through a setting on the customizer, but that will cause this problem to happen each time I change the number of items.

Does anyone have any clever ideas?

0

The get_theme_mod() function works the following:

  • It fetches the get_theme_mods() function under the hood.
  • This returns the following data

    $theme_slug = get_option( 'stylesheet' );
    get_option( "theme_mods_{$theme_slug}" );
    

So in case you upgrade, write an upgrade function specific for that version of your plugin or theme, that uses one of the following, where the name is fetched via get_option( 'stylesheet' ); again:

  • delete_option( "mods_{$theme_name}" );
  • update_option( "theme_mods_{$theme_slug}", $mods );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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