主题面板中的WordPress徽标上传选项

时间:2016-08-29 作者:neil

大家好,我在wordpress的主题面板中添加徽标选项时遇到了一个问题,我正在使用此代码

function logo_display()
{
    ?>
        <input type="file" name="logo" /> 
        <?php echo get_option(\'logo\'); ?>
   <?php
}
function handle_logo_upload()
{
    if(!empty($_FILES["demo-file"]["tmp_name"]))
    {
        $urls = wp_handle_upload($_FILES["logo"], array(\'test_form\' => FALSE));
        $temp = $urls["url"];
       return $temp;   
    }  
    return $option;
}
function display_theme_panel_fields()
{
    add_settings_section("section", "All Settings", null, "theme-options");
    add_settings_field("logo", "Logo", "logo_display", "theme-options", "section");  
    register_setting("section", "logo", "handle_logo_upload");
}
add_action("admin_init", "display_theme_panel_fields");
问题是它没有保存徽标,也没有在admin中显示它。我用不同的方法尝试了10次,但这段代码不起作用。请查看此代码并帮助我。

2 个回复
最合适的回答,由SO网友:shamim khan 整理而成

如果您使用wordpress自定义程序,请尝试以下代码

public static function register ( $wp_customize ) {
    // Logo upload
    $wp_customize->add_section( \'bia_logo_section\' , array(
        \'title\'       => __( \'Site Logo\', \'bia\' ),
        \'priority\'    => 30,
        \'description\' => \'Upload a logo to replace the default site name and description in the header\',
    ) );

    $wp_customize->add_setting( \'bia_logo\', array(
        \'sanitize_callback\' => \'esc_url_raw\',
    ) );

    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, \'bia_logo\', array(
        \'label\'    => __( \'Site Logo\', \'bia\' ),
        \'section\'  => \'bia_logo_section\',
        \'settings\' => \'bia_logo\',
    ) ) );
}
我想你也可以试试redux framework 用于管理面板选项

SO网友:cjbj

自版本4.5起Theme Logo 是WordPress的标准功能。您只需将以下代码添加到functions.php:

function wpse237461_theme_logo() {
    add_theme_support( \'custom-logo\', array(
        \'height\'      => 100,
        \'width\'       => 400,
        \'flex-width\' => true ) );
    }
add_action( \'after_setup_theme\', \'wpse237461_theme_logo\' );
现在,您可以在主题定制器中更改徽标,并可以使用将其添加到主题中the_custom_logo(). 无需自己处理文件和选项。