get_template_directory_uri() - WordPress 函数参考中文文档

返回介绍

get_template_directory_uri()

发布于 2017-09-11 字数4587 浏览 902 评论 0

get_template_directory_uri()

Retrieve theme directory URI.


描述


返回值

(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 );
}

更新日志

Version 描述
1.5.0 Introduced.

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' );
    

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!