上一篇文章將為文章添加特色功能的時候説到 add_theme_support 還可以為主題添加其它的功能支持。比如自定背景功能, 老版本的 WordPress 使用的是 add_theme_support() 函數,但是這個函數在 3.4 版本中已經棄用了,我們應該使用 add_theme_support 函數來添加自定義背景支持,方法很簡單,只需要往主題的 functions.php 中添加一下代碼即可:

  1. add_theme_support('custom-background');  

還可以為添加一些默認參數:

  1. $defaults = array(   
  2.     'default-color'          => '',   
  3.     'default-image'          => '',   
  4.     'wp-head-callback'       => '_custom_background_cb',   
  5.     'admin-head-callback'    => '',   
  6.     'admin-preview-callback' => ''  
  7. );   
  8. add_theme_support( 'custom-background', $defaults );  

如果你的主題不是私有的,那你就應該保證你的主題有普遍適用性,不能出錯,所以你應該先判斷一下 WordPress 的版本,在添加自定義背景功能支持:

  1. global $wp_version;   
  2. if ( version_compare( $wp_version, '3.4', '>=' ) )    
  3.     add_theme_support( 'custom-background' );    
  4. else  
  5.     add_custom_background( $args );  


添加代碼後的效果,就如同 WordPress 默認主題一般:

不過可不是添加代碼就行了哦,請確定你的主題 header.php 文件又 wp_head() 函數。