get_template_directory_uri()获取当前主题所在的绝对路径URL

目录

描述

译文

检索模板 目录URI

原文

Retrieve template directory URI for the current theme. Checks for SSL.

Note: Does not return a trailing slash following the directory address.

In the event that a child theme is being used, the parent theme directory URI will be returned. get_template_directory_uri() should be used for resources that are not intended to be included in/over-ridden by a child theme. Use get_stylesheet_directory_uri() to include resources that are intended to be included in/over-ridden by the child theme.

get_template_directory_uri() 描述

用法

Use the URI<?php get_template_directory_uri(); ?>

Output the URI<?php echo get_template_directory_uri(); ?>

get_template_directory_uri() 用法

参数

返回值

uri (string

Template directory URI.

get_template_directory_uri() 返回值

示例

Using get_template_directory_uri() to enqueue a script with the correct path

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
function my_scripts_method() {
	wp_enqueue_script(
		'custom_script',
		get_template_directory_uri() . '/js/custom_script.js',
		array('jquery')
	);
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

get_template_directory_uri() 示例

注意

get_template_directory_uri() 注意

历史

  • 添加于 版本: 1.5.0

get_template_directory_uri() 历史

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve theme directory URI.
 *
 * @since 1.5.0
 *
 * @return string Template directory URI.
 */
function get_template_directory_uri() {
	$template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
	$theme_root_uri = get_theme_root_uri( $template );
	$template_dir_uri = "$theme_root_uri/$template";

	/**
	 * Filter the current theme directory URI.
	 *
	 * @since 1.5.0
	 *
	 * @param string $template_dir_uri The URI of the current theme directory.
	 * @param string $template         Directory name of the current theme.
	 * @param string $theme_root_uri   The themes root URI.
	 */
	return apply_filters( 'template_directory_uri', $template_dir_uri, $template, $theme_root_uri );
}

get_template_directory_uri() 源文件

相关