get_theme_root()获取主题themes文件夹的绝对目录路径

目录

描述

译文

检索主题 目录的路径。

没有结尾斜线。

原文

Retrieves the absolute path to the themes directory, without the trailing slash.

get_theme_root() 描述

用法

<?php get_theme_root(); ?>

get_theme_root() 用法

参数

$stylesheet_or_template

(string) (必填) The stylesheet or template name of the theme.

默认值: None

get_theme_root() 参数

返回值

(string

Absolute path to the themes directory (without the trailing slash).

get_theme_root() 返回值

示例

Number of Subdirectories in Themes Directory

The function below informs about the number of subdirectories in the themes directory. Note that this doesn't necessarily match the number of themes recognized by WordPress.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
function display_themes_subdirs_count_info()
  $theme_root = get_theme_root();
  $files_array = glob("$theme_root/*", GLOB_ONLYDIR);
  echo "There are " . count($files_array) . " subdirectories in the " . $theme_root . " directory"; 
}
?>

Example output:

There are 5 subdirectories in the /home/user/public_html/wp-content/themes directory.

get_theme_root() 示例

注意

get_theme_root() 注意

历史

添加于 版本: 1.5.0

get_theme_root() 历史

源文件

get_theme_root() 函数的代码位于 wp-includes/theme.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve path to themes directory.
 *
 * Does not have trailing slash.
 *
 * @since 1.5.0
 *
 * @global array $wp_theme_directories
 *
 * @param string $stylesheet_or_template The stylesheet or template name of the theme
 * @return string Theme path.
 */
function get_theme_root( $stylesheet_or_template = false ) {
	global $wp_theme_directories;

	if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
		// Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
		// This gives relative theme roots the benefit of the doubt when things go haywire.
		if ( ! in_array( $theme_root, (array) $wp_theme_directories ) )
			$theme_root = WP_CONTENT_DIR . $theme_root;
	} else {
		$theme_root = WP_CONTENT_DIR . '/themes';
	}

	/**
	 * Filter the absolute path to the themes directory.
	 *
	 * @since 1.5.0
	 *
	 * @param string $theme_root Absolute path to themes directory.
	 */
	return apply_filters( 'theme_root', $theme_root );
}

get_theme_root() 源文件

相关