get_template_directory_uri()

get_template_directory_uri()

تابع get_template_directory_uri() برای برگرداندن مقدار URL از روت قالب می‌باشد و زمانی که شما نیاز دارید تا آدرسی URL سایت را استفاده کنید می‌توانید از این تابع استفاده کنید.

کارایی این تابع برای استفاده در هدر سایت و برای بارگذاری فایلهای CSS, js خارجی که در تگ header فراخوانی می‌شود قابل استفاده است.

منبع get_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 );
}

نکته مهم: اگر شما از Child theme استفاده می‌کنید این تابع مقدار صحیح را به شما برنمی گرداند و برای نمایش مقدار صحیح باید از تابع get_stylesheet_directory_uri() استفاده نمایید.

یک نمونه از نحوه استفاده از تابع get_template_uri برای آدرس دهی به یک تصویر استاتیک

<img src="<?php echo get_template_directory_uri(); ?>/images/logo.png" width="" height="" alt="" />