自定义首页图片为bing每日美图

将workpress 自定义为bing搜索的背景图片的方法,我这里改的是twentyseventeen theme。

将站点下的wp-content/themes/twentyseventeen/inc/custom-header.php中

twentyseventeen_custom_header_setup函数修改为如下函数:

function twentyseventeen_custom_header_setup() {
        //bing api
        $url = 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=';
        $resolution = '1920x1080';

        // language options
        $locale = array(
                        "zh-CN",
                        "en-US",
                        "ja-JP",
                        "en-AU",
                        "en-UK",
                        "de-DE",
                        "en-NZ"
                       );

        // parameters
        foreach ($locale as $lang) {
                $URL = $url.$lang;
    //print_r($URL);
                $data = file_get_contents($URL);
    if( $data === FALSE){
                 	continue;
    }
                $json = json_decode(trim($data), true);
                if ($json) {
                        $images = $json['images'];
                        foreach ($images as $image) {
                                $urlbase = $image['urlbase'];
                                $image_url = 'https://www.bing.com' . $urlbase . '_' . $resolution . '.jpg';
        break;
                                // $copyright = $image['copyright'];
                                // now you can save the picture at $image_url with the title $copyright
                        }
                }
    if($image_url){
      break;
    }
        }

        //此函数来源于原主题下的/inc/custom-header.php
        add_theme_support( 'custom-header', apply_filters( 'twentyseventeen_custom_header_args', array(
                                        'default-image'      => $image_url,
                                        'width'              => 2000,
                                        'height'             => 1200,
                                        'flex-height'        => true,
                                        'video'              => true,
                                        'wp-head-callback'   => 'twentyseventeen_header_style',
                                        ) ) );
       register_default_headers( array(
                'default-image' => array(
                        'url'           => $image_url,
                        'thumbnail_url' => $image_url,
                        'description'   => __( 'Default Header Image', 'twentyseventeen' ),
                ),
        ) );

}
add_action( 'after_setup_theme', 'twentyseventeen_custom_header_setup' );

参考:

自定义 Twenty Seventeen 页头媒体

 

Published
Categorized as web

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.