Loop get_theme_mod

时间:2014-04-30 作者:davidcondrey

我已经设置了一系列自定义设置

与其为每个设置单独调用单独的get\\u theme\\u mod语句,是否有一种方法可以简化它,使其循环通过它们,而不必显式地单独编写每个设置?

完整文件也在上设置Gist 供审查

<?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 );
    }
}

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

get_them_mods() 这将。。。

检索当前主题的所有主题修改值。

如果没有设置主题mods,将返回布尔值false。

https://codex.wordpress.org/Function_Reference/get_theme_mods

它应该返回array 你可以绕过去。

结束

相关推荐

Count post views in loop

我正在尝试显示博客页面上每个帖子的帖子视图(所以在循环中)。我在函数中使用以下代码进行了尝试。php: // function to count views. function setAndViewPostViews($postID) { $count_key = \'views\'; $count = get_post_meta($postID, $count_key, true); if($count==\'\'){ $