J'ai un tas de paramètres de personnalisation mis en place
Plutôt que d'appeler individuellement des instructions get_theme_mod pour chaque paramètre, existe-t-il un moyen de le simplifier pour les parcourir en boucle sans qu'il soit nécessaire d'écrire explicitement chacune d'elles individuellement?
Le fichier complet est également configuré sur Gist pour examen
<?php
/** Adds the Customize page to the WordPress admin area */
function litho_customizer_menu() {
add_theme_page( 'Customize', 'Customize', 'edit_theme_options', 'customize.php' );
}
add_action( 'admin_menu', 'litho_customizer_menu' );
/** Adds the individual sections, settings, and controls to the theme customizer */
function litho_customizer( $wp_customize ) {
class Example_Customize_Textarea_Control extends WP_Customize_Control {
public $type = 'textarea';
public function render_content() { ?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
</label>
<?php }
}
$wp_customize->add_section( 'site_settings', array(
'title' => 'Framework Settings',
'description' => 'Adjust settings',
'priority' => 35,
));
/* <?php echo $sidebar_position; ?> */
$wp_customize->add_setting( 'site-subtitle', array( 'default' => 'Default subtitle text','sanitize_callback' => 'litho_sanitize_text', ) );
$wp_customize->add_control('site-subtitle', array(
'label' => 'Copyright text',
'section' => 'site_settings',
'type' => 'text'
));
$wp_customize->add_setting( 'litho_copyright_textbox',array( 'default' => 'Default copyright text','sanitize_callback' => 'litho_sanitize_text', ) );
$wp_customize->add_control( 'litho_copyright_textbox',array(
'label' => 'Copyright text',
'section' => 'site_settings',
'type' => 'text'
));
$wp_customize->add_setting( 'litho_hide_copyright',array('sanitize_callback' => 'litho_sanitize_checkbox',) );
$wp_customize->add_control( 'litho_hide_copyright',array(
'type' => 'checkbox',
'label' => 'Hide copyright text',
'section' => 'site_settings'
));
$wp_customize->add_setting( 'litho_logo_placement', array( 'default' => 'left','sanitize_callback' => 'litho_sanitize_logo_placement', ) );
$wp_customize->add_control( 'litho_logo_placement',array(
'type' => 'radio',
'label' => 'Logo placement',
'section' => 'site_settings',
'choices' => array( 'left' => 'Left','right' => 'Right','center' => 'Center')
));
$wp_customize->add_setting( 'litho_powered_by', array( 'default' => 'wordpress','sanitize_callback' => 'litho_sanitize_powered_by', ) );
$wp_customize->add_control( 'litho_powered_by',array(
'type' => 'select',
'label' => 'This site is powered by:',
'section' => 'site_settings',
'choices' => array( 'wordpress' => 'WordPress','nuclear-energy' => 'Nuclear Energy')
));
$wp_customize->add_setting( 'page-setting', array( 'sanitize_callback' => 'litho_sanitize_integer', ) );
$wp_customize->add_control( 'page-setting',array(
'type' => 'dropdown-pages',
'label' => 'Choose a page:',
'section' => 'site_settings'
));
$wp_customize->add_setting( 'color-setting', array( 'default' => '#000000','sanitize_callback' => 'sanitize_hex_color', ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'color-setting',array(
'label' => 'Color Setting',
'section' => 'site_settings',
'settings' => 'color-setting'
)));
$wp_customize->add_setting( 'file-upload' );
$wp_customize->add_control( new WP_Customize_Upload_Control( $wp_customize, 'file-upload',array(
'label' => 'File Upload',
'section' => 'site_settings',
'settings' => 'file-upload'
)));
$wp_customize->add_setting( 'img-upload' );
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'img-upload',array(
'label' => 'Image Upload',
'section' => 'site_settings',
'settings' => 'img-upload'
)));
$wp_customize->add_setting( 'textarea' );
$wp_customize->add_control( new Example_Customize_Textarea_Control( $wp_customize, 'textarea',array(
'label' => 'Textarea',
'section' => 'site_settings',
'settings' => 'textarea'
)));
$wp_customize->add_setting( 'font-color', array( 'default' => '#444444', 'sanitize_callback' => 'sanitize_hex_color', ));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'font-color',array(
'label' => 'Font Color',
'section' => 'colors',
'settings' => 'font-color'
)));
$wp_customize->add_setting( 'featured-background',array( 'default' => '#ffffff','sanitize_callback' => 'sanitize_hex_color','transport' => 'postMessage', ));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize,'featured-background',array(
'label' => 'Featured Background',
'section' => 'colors',
'settings' => 'featured-background'
)));
/* AJAX function for live preview */
if ( $wp_customize->is_preview() && ! is_admin() ) {
add_action( 'wp_footer', 'litho_customize_preview', 21);
}
function litho_customize_preview() {
wp_enqueue_script( 'litho-customize-js', get_template_directory_uri().'/core/js/customize.js',array());
}
add_action( 'wp_enqueue_scripts', 'litho_customize_preview' );
$wp_customize->get_setting('blogname')->transport='postMessage';
}
add_action( 'customize_register', 'litho_customizer' );
function litho_header_alignment() {
$litho_position = get_theme_mod( 'litho_logo_placement' );
if( $litho_position != '' ) {
switch ( $litho_position ) {
case 'left':
break;
case 'right':
echo '<style type="text/css">';
echo '#header #logo{ float: right; }';
echo '</style>';
break;
case 'center':
echo '<style type="text/css">';
echo '#header{ text-align: center; }';
echo '#header #logo{ float: none; margin-left: auto; margin-right: auto; }';
echo '</style>';
break;
}
}
}
add_action ( 'wp_head','litho_header_alignment' );
/* The force_balance_tags() function ensures that no tags are left unclosed, while the wp_kses_post() ensures that only safe tags make it into the database (the same tags that are allowed in a standard WordPress post */
function litho_sanitize_text( $input ) {
return wp_kses_post( force_balance_tags( $input ) );
}
function litho_sanitize_checkbox( $input ) {
if ( $input == 1 ) {
return 1;
} else {
return '';
}
}
function litho_sanitize_logo_placement( $input ) {
$valid = array(
'left' => 'Left',
'right' => 'Right',
'center' => 'Center',
);
if ( array_key_exists( $input, $valid ) ) {
return $input;
} else {
return '';
}
}
function litho_sanitize_powered_by( $input ) {
$valid = array(
'wordpress' => 'WordPress',
'nuclear-energy' => 'Nuclear Energy',
);
if ( array_key_exists( $input, $valid ) ) {
return $input;
} else {
return '';
}
}
function litho_sanitize_integer( $input ) {
if( is_numeric( $input ) ) {
return intval( $input );
}
}
Il y a get_them_mods()
qui va ...
Récupérez toutes les valeurs de modification de thème pour le thème actuel.
Si aucun mods de thème n'a été défini, le résultat sera boolean false.
https://codex.wordpress.org/Function_Reference/get_theme_mods
Il devrait retourner une array
sur laquelle vous pouvez faire une boucle.