返回介绍

get_template_directory_uri()

发布于 2017-09-11 00:19:58 字数 4587 浏览 1400 评论 0 收藏 0

get_template_directory_uri()

Retrieve theme directory URI.


description


返回值

(string) Template directory URI.


源代码

File: wp-includes/theme.php

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";

	/**
	 * Filters 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 );
}

更新日志

Versiondescription
1.5.0Introduced.

More Information

Notes

  • Checks for SSL
  • Does not return a trailing slash following the directory address

相关函数

Uses

  • wp-includes/theme.php: get_theme_root_uri()
  • wp-includes/theme.php: get_template()
  • wp-includes/theme.php: template_directory_uri
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/link-template.php: get_parent_theme_file_uri()
  • wp-includes/link-template.php: get_theme_file_uri()
  • wp-includes/theme.php: get_editor_stylesheets()
  • wp-admin/custom-header.php: Custom_Image_Header::get_default_header_images()
  • wp-admin/custom-header.php: Custom_Image_Header::step_1()
  • wp-admin/custom-header.php: Custom_Image_Header::reset_header_image()
  • wp-admin/custom-header.php: Custom_Image_Header::process_default_headers()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::register_controls()
  • wp-includes/theme.php: _get_random_header_data()
  • wp-includes/theme.php: get_custom_header()
  • wp-includes/theme.php: get_theme_mod()
  • wp-includes/general-template.php: get_bloginfo()
  • Show 7 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 11You must log in to vote on the helpfulness of this note Contributed by Florian Simeth

    This function returns the URL to the root theme. If a child theme is used and you want to return the URL to the current child theme, use get_stylesheet_directory_uri() instead.

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

    
    add_action('wp_enqueue_scripts', 'wpdocs_scripts_method');
    
    /*
     * Enqueue a script with the correct path.
     */
    function wpdocs_scripts_method() {
    	wp_enqueue_script(
    		'custom_script',
    		get_template_directory_uri() . '/js/custom_script.js',
    		array('jquery')
    	);
    }
    

    Using get_template_directory_uri() to link a static image with its correct path in html :

    
    <img src="<?php echo get_template_directory_uri(); ?>/images/logo.png" width="" height="" alt="" />
    
    
    /**
     * Enqueue scripts and styles.
     */
    function wpdocs_theme_slug_scripts() {
    	// Custom scripts require a unique slug (Theme Name).
    	wp_enqueue_script( 'theme-slug-custom-script', get_template_directory_uri() . '/js/custom-script.js', array(), '1.0.0', true );
    
    	/*
    	 * To avoid double loading Genericons will not need a slug. Same applies
    	 * to all other non-custom styles or scripts.
    	 */
    	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '1.0.0' );
    }
    add_action( 'wp_enqueue_scripts', 'wpdocs_theme_slug_scripts' );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文