current_theme_supports

函式
current_theme_supports ( $feature, $args )
引數
  • (string) $feature The feature being checked. See add_theme_support() for the list of possible values.
    Required:
  • (mixed) $args Optional extra arguments to be checked against certain features.
    Required:
返回值
  • (bool) True if the active theme supports the feature, false otherwise.
定義位置
相關方法
get_theme_supportremove_theme_supportcurrent_theme_info_remove_theme_supportadd_theme_support
引入
2.9.0
棄用
-

current_theme_supports: 這個函式檢查當前主題是否支援一個給定的功能,如文章縮圖或自定義背景。它可以在外掛和主題開發中使用,以確保在嘗試使用某項功能之前是可用的。

檢查一個主題對某一特定功能的支援。

使用示例:

current_theme_supports( ‘custom-logo’);
current_theme_supports( ‘html5’, ‘comment-form’);

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function current_theme_supports( $feature, ...$args ) {
global $_wp_theme_features;
if ( 'custom-header-uploads' === $feature ) {
return current_theme_supports( 'custom-header', 'uploads' );
}
if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
return false;
}
// If no args passed then no extra checks need to be performed.
if ( ! $args ) {
/** This filter is documented in wp-includes/theme.php */
return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
switch ( $feature ) {
case 'post-thumbnails':
/*
* post-thumbnails can be registered for only certain content/post types
* by passing an array of types to add_theme_support().
* If no array was passed, then any type is accepted.
*/
if ( true === $_wp_theme_features[ $feature ] ) { // Registered for all types.
return true;
}
$content_type = $args[0];
return in_array( $content_type, $_wp_theme_features[ $feature ][0], true );
case 'html5':
case 'post-formats':
/*
* Specific post formats can be registered by passing an array of types
* to add_theme_support().
*
* Specific areas of HTML5 support *must* be passed via an array to add_theme_support().
*/
$type = $args[0];
return in_array( $type, $_wp_theme_features[ $feature ][0], true );
case 'custom-logo':
case 'custom-header':
case 'custom-background':
// Specific capabilities can be registered by passing an array to add_theme_support().
return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] );
}
/**
* Filters whether the active theme supports a specific feature.
*
* The dynamic portion of the hook name, `$feature`, refers to the specific
* theme feature. See add_theme_support() for the list of possible values.
*
* @since 3.4.0
*
* @param bool $supports Whether the active theme supports the given feature. Default true.
* @param array $args Array of arguments for the feature.
* @param string $feature The theme feature.
*/
return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
function current_theme_supports( $feature, ...$args ) { global $_wp_theme_features; if ( 'custom-header-uploads' === $feature ) { return current_theme_supports( 'custom-header', 'uploads' ); } if ( ! isset( $_wp_theme_features[ $feature ] ) ) { return false; } // If no args passed then no extra checks need to be performed. if ( ! $args ) { /** This filter is documented in wp-includes/theme.php */ return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores } switch ( $feature ) { case 'post-thumbnails': /* * post-thumbnails can be registered for only certain content/post types * by passing an array of types to add_theme_support(). * If no array was passed, then any type is accepted. */ if ( true === $_wp_theme_features[ $feature ] ) { // Registered for all types. return true; } $content_type = $args[0]; return in_array( $content_type, $_wp_theme_features[ $feature ][0], true ); case 'html5': case 'post-formats': /* * Specific post formats can be registered by passing an array of types * to add_theme_support(). * * Specific areas of HTML5 support *must* be passed via an array to add_theme_support(). */ $type = $args[0]; return in_array( $type, $_wp_theme_features[ $feature ][0], true ); case 'custom-logo': case 'custom-header': case 'custom-background': // Specific capabilities can be registered by passing an array to add_theme_support(). return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] ); } /** * Filters whether the active theme supports a specific feature. * * The dynamic portion of the hook name, `$feature`, refers to the specific * theme feature. See add_theme_support() for the list of possible values. * * @since 3.4.0 * * @param bool $supports Whether the active theme supports the given feature. Default true. * @param array $args Array of arguments for the feature. * @param string $feature The theme feature. */ return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores }
function current_theme_supports( $feature, ...$args ) {
	global $_wp_theme_features;

	if ( 'custom-header-uploads' === $feature ) {
		return current_theme_supports( 'custom-header', 'uploads' );
	}

	if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
		return false;
	}

	// If no args passed then no extra checks need to be performed.
	if ( ! $args ) {
		/** This filter is documented in wp-includes/theme.php */
		return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
	}

	switch ( $feature ) {
		case 'post-thumbnails':
			/*
			 * post-thumbnails can be registered for only certain content/post types
			 * by passing an array of types to add_theme_support().
			 * If no array was passed, then any type is accepted.
			 */
			if ( true === $_wp_theme_features[ $feature ] ) {  // Registered for all types.
				return true;
			}
			$content_type = $args[0];
			return in_array( $content_type, $_wp_theme_features[ $feature ][0], true );

		case 'html5':
		case 'post-formats':
			/*
			 * Specific post formats can be registered by passing an array of types
			 * to add_theme_support().
			 *
			 * Specific areas of HTML5 support *must* be passed via an array to add_theme_support().
			 */
			$type = $args[0];
			return in_array( $type, $_wp_theme_features[ $feature ][0], true );

		case 'custom-logo':
		case 'custom-header':
		case 'custom-background':
			// Specific capabilities can be registered by passing an array to add_theme_support().
			return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] );
	}

	/**
	 * Filters whether the active theme supports a specific feature.
	 *
	 * The dynamic portion of the hook name, `$feature`, refers to the specific
	 * theme feature. See add_theme_support() for the list of possible values.
	 *
	 * @since 3.4.0
	 *
	 * @param bool   $supports Whether the active theme supports the given feature. Default true.
	 * @param array  $args     Array of arguments for the feature.
	 * @param string $feature  The theme feature.
	 */
	return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}

常見問題

FAQs
檢視更多 >