函数参考/add theme support

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn includeonlydiv style=clear:both; background-color:#FAEBD7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This page is marked as incomplete. You can help Codex by expanding it./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude

描述

Allows a theme or plugin to register support of a certain 主题特性. If called from a theme, it should be done in the theme's functions.php file to work. It can also be called from a plugin if attached to a hook.

If attached to a hook, it must be after_setup_theme. The init hook may be too late for some features.

用法

%%%<?php add_theme_support( $feature ); ?>%%%

参数

tt$feature/tt
(string) (required) Name for the feature being added.
Features list:
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Addable Features

Post Formats

This feature enables Post Formats support for a Theme. This feature became available with Version 3.1. When using Child Themes, be aware that add_theme_support( 'post-formats' ) will override the formats as defined by the parent theme, not add to it.

To enable the specific formats (see supported formats at Post Formats), use:

add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

To check if there is a 'quote' post format assigned to the post, use

// in your theme single.php, page.php or custom post type
if ( has_post_format( 'quote' ) ) {
	echo 'This is a quote.';
}

Post Thumbnails

This feature enables Post Thumbnails support for a Theme. The feature became available with Version 2.9. Note that you can optionally pass a second argument with an array of the Post Types for which you want to enable this feature.

add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails', array( 'post' ) );          // Posts only
add_theme_support( 'post-thumbnails', array( 'page' ) );          // Pages only
add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); // Posts and Movies

This feature must be called before the init hook is fired. That means it needs to be placed directly into functions.php or within a function attached to the 'after_setup_theme' hook. For custom post types, you can also add post thumbnails using the register_post_type function as well.

To display thumbnails in themes index.php or single.php or custom templates, use:

the_post_thumbnail();

To check if there is a post thumbnail assigned to the post before displaying it, use:

if ( has_post_thumbnail() ) {
	the_post_thumbnail();
}

自定义背景

This feature enables Custom_Backgrounds support for a theme as of Version 3.4.

add_theme_support( 'custom-background' );

Note that you can add default arguments using:

$defaults = array(
	'default-color'          => '',
	'default-image'          => '',
	'wp-head-callback'       => '_custom_background_cb',
	'admin-head-callback'    => '',
	'admin-preview-callback' => ''
);
add_theme_support( 'custom-background', $defaults );
  • To make this backwards compatible you can use this check to determine if WordPress is at least version 3.4 or not. So during the transition to 3.4, you can support both functions by using them in the alternative:
global $wp_version;
if ( version_compare( $wp_version, '3.4', '>=' ) ) 
	add_theme_support( 'custom-background' ); 
else
	add_custom_background( $args );

自定义顶部

This feature enables 自定义顶部 support for a theme as of 3.4 版本.

add_theme_support( 'custom-header' );

请注意您可以添加的默认参数列表:

$defaults = array(
	'default-image'          => '',     //默认图像
	'random-default'         => false,  //是否默认随机
	'width'                  => 0,      //宽度
	'height'                 => 0,      //高度
	'flex-height'            => false,
	'flex-width'             => false,
	'default-text-color'     => '',     //默认文本颜色
	'header-text'            => true,   //顶部文本开关
	'uploads'                => true,   //是否允许上传
	'wp-head-callback'       => '',
	'admin-head-callback'    => '',
	'admin-preview-callback' => '',
);
add_theme_support( 'custom-header', $defaults );
  • To make this backwards compatible you can use this check to determine if WordPress is at least version 3.4 or not. So during the transition to 3.4, you can support both functions by using them in the alternative:
global $wp_version;
if ( version_compare( $wp_version, '3.4', '>=' ) )
	add_theme_support( 'custom-header' );
else
	add_custom_image_header( $args );

Feed Links

This feature enables post and comment RSS feed links to head. This should be used in place of the deprecated automatic_feed_links() function. This feature became available with Version 3.0.

add_theme_support( 'automatic-feed-links' );

Multisite

To show the "Featured Image" meta box in multisite installation, make sure you update the allowed upload file types, in Network Admin, Network Admin Settings SubPanel#Upload_Settings, Media upload buttons options. Default is off.

Notes

The following parameters are read only, and should only be used in the context of current_theme_supports():

更新日志

源文件

add_theme_support() is located in onlyincludecodewp-includes/theme.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

相关

模板:Theme Support

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude