douchun1859 2017-12-25 12:31
浏览 73
已采纳

Wordpress get_option问题

I was looking a tutorial to use the customizer on wordpress for a theme from scratch, but I have some issues with get_option, I have no return.

I put my code below.

<?php
//==================================================
//============= Chargement des scripts =============
//==================================================

define('schweitzer_ver', '0.2');


// Chargement front end
function schweitzer_scripts(){

    // Chargement des styles
    //wp_enqueue_style( 'styles-bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css',
    //  '', schweitzer_ver, 'all' );
    wp_enqueue_style( 'styles', get_template_directory_uri() . '/style.css',
        array(), schweitzer_ver, 'all' );
    wp_enqueue_style( 'menu', get_template_directory_uri() . '/css/menu.css',
        array(), schweitzer_ver, 'all' );


    // Chargement des scripts
    wp_enqueue_script( 'schweitzer_script', get_template_directory_uri() . '/js/schweitzer.js',
        array('jquery'), schweitzer_ver, true );
    wp_enqueue_script( 'menu_script', get_template_directory_uri() . '/js/menu.js',
        array('jquery'), schweitzer_ver, true );
    }

    // Intégration et mise en forme du menu
    function clean_custom_menus() {
        $menu_name = 'primary';
        if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
            $menu = wp_get_nav_menu_object($locations[$menu_name]);
            $menu_items = wp_get_nav_menu_items($menu->term_id);
            // $menu_list = '<nav>' ."
";
            $menu_list .= "\t\t\t\t". '<ul>' ."
";

        foreach ((array) $menu_items as $key => $menu_item) {
            $title = $menu_item->title;
            $url = $menu_item->url;
            $menu_list .= "\t\t\t\t\t". '<li><a href="'. $url .'">'. $title .'</a></li>' ."
";
            }

        $menu_list .= "\t\t\t\t". '</ul>' ."
";
        $menu_list .= "\t\t\t". '</nav>' ."
";
        }

        else {
        $menu_list = 'Pas de menu défini';
        }
    echo $menu_list;
}



add_action( 'wp_enqueue_scripts', 'schweitzer_scripts', 'clean_custom_menus');


//==================================================
//================= Configuration ==================
//==================================================

function schweitzer_setup ()
{

    // Active gestion des menus (avec plusieurs positions)
    register_nav_menus(array('primary'=>'principal'));

    // support des vignettes
    add_theme_support('post-thumbnails');

    // Retire générateur de version (sécurité !)
    remove_action('wp_head', 'wp_generator');

    // Retire guillemets français
    remove_filter ('the_content', 'wptexturize');

    // Support du titre géré par WP (meilleur SEO)
    add_theme_support('title-tag');

}

    add_action( 'after_setup_theme', 'schweitzer_setup' );

//==================================================
//================ Options du theme ================
//==================================================

function schweitzer_customizer()
{

    //===== Variables =====
    // Couleur du menu
    $menu_color = get_option('menu_color','#2a2a2a');

    // Transparence du menu
    $menu_opacity = get_option('menu_opacity','0.5');

    // Nombre d'éléments dans le menu
    // $menu_element = get_option( 'menu_element' );

    ?>
    <style>
        nav li:nth-of-type(1){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(2){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(3){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(4){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
        nav li:nth-of-type(5){background-color: <?php echo $menu_color; ?>; opacity: <?php echo $menu_opacity; ?>}
    </style>
    <?php

}
    add_action( 'wp_head', 'schweitzer_customizer' );




function schweitzer_customize_register( $wp_customize ) {

        $wp_customize->add_section( 'menu_options' , array(
        'title' =>  'Réglage du Menu',
    ) );

    $wp_customize->add_setting( 'menu_color' , array(
        'default' => '#000',
        'sanitize_callback' => 'sanitize_hex_color',
    ) );

    $wp_customize->add_setting( 'menu_opacity' , array(
        'default' => '0.5',
    ) );

    $wp_customize->add_control(
        new WP_Customize_Color_Control($wp_customize,
            'menu_color_selection',
            array('label' => 'Choix de la couleur',
                'section' => 'menu_options',
                'settings' => 'menu_color',
        )
    ));

    $wp_customize->add_control(
        'menu_opacity_selection',
        array(
            'label'    => __( 'Transparence', 'schweitzer' ),
            'section'  => 'menu_options',
            'settings' => 'menu_opacity',
            'type'     => 'radio',
            'choices'  => array(
                '0'  => '0% (opaque)',
                '0.1' => '10 %',
                '0.2'  => '20 %',
                '0.3' => '30 %',
                '0.4'  => '40 %',
                '0.5' => '50 %',
                '0.6'  => '60 %',
                '0.7' => '70 %',
                '0.8' => '80 %',
                '0.9'  => '90 %',
                '1' => '100 % (transparent)',),
        )
    );
}
add_action( 'customize_register', 'schweitzer_customize_register' );

function schweitzer_register_settings() {
    register_setting( 'menu_settings', 'my_option_name', 'intval' );
}

add_action( 'admin_init', 'schweitzer_register_settings' );

This is my functions.php.

The back end seems to work well but there is no trace of my "menu_color" options.

  • 写回答

1条回答 默认 最新

  • doupang5433 2017-12-25 20:03
    关注

    For theme options modified through the $wp_customize, use get_theme_mod() instead of get_option() to retrieve the value.

    The difference is, that for the latter the settings are preserved per theme and thus you could possibly have different options on different themes you might switch on occasion.

    To have a fallback value when the setting is unchanged or not set, you can use the default-value as you already do in your code:

    $wp_customize->add_setting('menu_color', array(
            'default' => '#000',
            'sanitize_callback' => 'sanitize_hex_color',
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧