locate_template

函式
locate_template ( $template_names, $load = false, $require_once = true, $args = array() )
引數
  • (string|array) $template_names Template file(s) to search for, in order.
    Required:
  • (bool) $load If true the template file will be loaded if it is found.
    Required:
    Default: false
  • (bool) $require_once Whether to require_once or require. Has no effect if `$load` is false. Default true.
    Required:
    Default: true
  • (array) $args Optional. Additional arguments passed to the template. Default empty array.
    Required:
    Default: array()
返回值
  • (string) The template filename if one is located.
定義位置
相關方法
load_templatelocate_block_templateget_date_templateget_templateget_block_template
引入
2.7.0
棄用
-

locate_template: 這是WordPress中的一個函式,允許你在你的主題或外掛中找到一個模板檔案。你可以用這個函式為特定的頁面、文章型別或其他內容以程式設計方式定位模板檔案,然後在你的程式碼中包含或修改它。

檢索存在的最高優先順序模板檔案的名稱。

在STYLESHEETPATH中搜尋,在TEMPLATEPATH和wp-includes/theme-compat之前,這樣繼承自父主題的主題就可以只過載一個檔案。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) { $located = ''; foreach ( (array) $template_names as $template_name ) { if ( ! $template_name ) { continue; } if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) { $located = STYLESHEETPATH . '/' . $template_name; break; } elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) { $located = TEMPLATEPATH . '/' . $template_name; break; } elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) { $located = ABSPATH . WPINC . '/theme-compat/' . $template_name; break; } } if ( $load && '' !== $located ) { load_template( $located, $require_once, $args ); } return $located; }
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
	$located = '';
	foreach ( (array) $template_names as $template_name ) {
		if ( ! $template_name ) {
			continue;
		}
		if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
			$located = STYLESHEETPATH . '/' . $template_name;
			break;
		} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
			$located = TEMPLATEPATH . '/' . $template_name;
			break;
		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
			break;
		}
	}

	if ( $load && '' !== $located ) {
		load_template( $located, $require_once, $args );
	}

	return $located;
}

常見問題

FAQs
檢視更多 >