하위 디렉터리의 워드프레스 템플릿 파일
얼마 전에 나는 새로운 워드프레스 프로젝트를 시작했다.그런데 문제가 생겼어요.디자인이 여러 개 있기 때문에 페이지, 투고 및 텍스트 형식의 출력에 여러 개의 템플릿을 다른 페이지 템플릿에 작성해야 합니다.
그래서 템플릿파일이 너무 많아서 서브디렉토리를 만들고 싶었어요.Wordpress 3.4 이후부터는 서브디렉토리 이름을 사용할 수 있습니다.page-templates모든 페이지 템플릿에 사용할 수 있지만 포맷 파일과 포스트 파일에 사용할 수 있는 방법은 없습니다.
네, 다음과 같은 기능을 추가하려고 했습니다.
get_template_part('/template-parts/page-templates' , 'page');
그리고.
require( get_template_directory() . '/template-parts/template-tags.php' );
만들고 싶은 이상적인 디렉토리 구조는 다음과 같습니다.
wp-content/themes/mytheme
- archive
- 404
- CSS
- JS
- Images
- template-parts (dir)
-- page-templates (dir for page-template files.)
-- format-templates (dir for format-templates.)
-- post-templates (dir for post-templates.)
- header
- footer
확실히 하자면.위와 같은 템플릿 파일 구조를 만들고 싶습니다.다음과 같은 폴더는 신경 쓰지 마십시오.CSS등이 올바르게 설정되어 있습니다.그 목적은 구조체를 성공적으로 만든 후 페이지 템플과 같은 템플릿을 선택할 수 있도록 하는 것입니다./wp-admin페이지 섹션을 편집합니다.
WP_Themeclass에는 메서드가 포함되어 있습니다.get_page_templates()다음 필터를 제공합니다.
apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
워드프레스에서는post그리고.page는,
add_filter( "theme_post_templates", ...그리고.add_filter( "theme_page_templates",...유효해야 합니다.
이 방법에 대한 코덱의 "사용자" 섹션에는 다음과 같이 기재되어 있습니다.
wp-admin/filters/filters.php: page_facebook_facebook()
wp-admin/filename/filename-filename.php: page_interval_box()
그래서 제가 생각하기에 이 두 사람이 다른 곳에서/wp-admin/페이지 섹션을 편집합니다.
필터에 관한 코어 파일의 정보:
* Filters list of page templates for a theme.
*
* The dynamic portion of the hook name, `$post_type`, refers to the post type.
* @since 4.7.0 Added the `$post_type` parameter.
*
* @param array $post_templates Array of page templates. Keys are filenames,
* values are translated names.
* @param WP_Theme $this The theme object.
* @param WP_Post|null $post The post being edited, provided for context, or null.
* @param string $post_type Post type to get the templates for.
여기서 상대적인 uri가 기능하는지, 필요한지는 잘 모르겠습니다.get_theme_file_path()전자를 상정하고 있습니다.
function deep_templates( $post_templates, $theme, $post, $post_type )
$post_templates['/folder/folder/folder/file.php'] = "Page Style One";
$post_templates['/folder/other-folder/file.php'] = "Page Style Two";
return $post_templates;
add_filter( 'theme_page_templates', 'deep_templates' );
및/또는
add_filter( 'theme_post_templates', 'deep_templates' );
add_filter( 'theme_my-custom-cpt_templates', 'deep_templates' );
여기 당신이 해야 할 일의 예가 있습니다.
- directory1 (dir)
-- directory2 (dir)
--- template-file.php
를 사용하여 이 템플릿에 액세스하려면get_template_part다음과 같이 할 수 있습니다.
get_template_part( 'directory1/directory2/template', 'file' );
'directory1/directory2에 디렉토리 구조를 정의합니다. template'이 문자열은 다음 문자열입니다.-템플릿명으로 지정합니다.'file'은 다음 문자열입니다.dash템플릿명으로 지정합니다.
그래서 당신의 경우,page-contact.php당신의 안에서page-templates디렉토리, 를 사용합니다.
get_template_part('template-parts/page-templates/page' , 'contact');
나는 이것이 이미 답이 있는 오래된 질문이라는 것을 알지만 그것은 내가 찾던 해결책이 아니었다.다른 사용자가 모든 WordPress 템플릿 파일을 하위 폴더로 이동하는 방법을 찾는 경우 다른 옵션 추가Reddit에서 WordPress 템플릿 파일의 기본 폴더를 변경하기 위해 이 코드를 발견하여 몇 가지 사항을 수정했습니다.이 코드는 함수에 넣을 수 있습니다.php를 선택합니다.
/**
* Tell WordPress that we have moved default template files to the page-templates folder.
* Based on code from: https://www.reddit.com/r/Wordpress/comments/ffhjvw/moving_wordpress_theme_template_files_to/
*
* Related posts with other solutions:
* https://stackoverflow.com/questions/60589503/moving-wordpress-theme-template-files-to-subdirectory
* https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
* https://wordpress.stackexchange.com/questions/291725/store-page-template-files-in-a-subfolder
* https://wordpress.stackexchange.com/questions/312159/how-to-move-page-template-files-like-page-slug-php-to-a-sub-directory/312611#312611
*
* Template hierarchy info:
* https://developer.wordpress.org/reference/hooks/type_template_hierarchy/
* https://core.trac.wordpress.org/browser/tags/5.8.1/src/wp-includes/template.php
* https://developer.wordpress.org/themes/basics/organizing-theme-files/#page-templates-folder
*
* @param array $templates A list of candidates template files.
* @return string Full path to template file.
*/
function change_template_path($templates) {
// The custom sub-directory for page templates in your theme.
$custom_sub_dir = 'page-templates';
// Don't use the custom template directory in unexpected cases.
if(empty($templates) || ! is_array($templates)) {
return $templates;
}
$page_template_id = 0;
$count = count( $templates);
if($templates[0] === get_page_template_slug()) {
// if there is a custom template, then our page-{slug}.php template is at the next index
$page_template_id = 1;
}
// The last one in $templates is page.php, single.php, or archives.php depending on the type of template hierarchy being read.
// Paths of all items starting from $page_template_id will get updated
for($i = $page_template_id; $i < $count ; $i++) {
$templates[$i] = $custom_sub_dir . '/' . $templates[$i];
}
return $templates;
}
// Add filters to override the path for each WP template hierarchy.
// These are all the template hierarchy filters. You should probably only override the ones you need.
add_filter('404_template_hierarchy', 'change_template_path');
add_filter('archive_template_hierarchy', 'change_template_path');
add_filter('attachment_template_hierarchy', 'change_template_path');
add_filter('author_template_hierarchy', 'change_template_path');
add_filter('category_template_hierarchy', 'change_template_path');
add_filter('date_template_hierarchy', 'change_template_path');
add_filter('embed_template_hierarchy', 'change_template_path');
add_filter('frontpage_template_hierarchy', 'change_template_path');
add_filter('home_template_hierarchy', 'change_template_path');
// If you override the index hierarchy, be sure to add an index.php template in your custom template folder.
add_filter('index_template_hierarchy', 'change_template_path');
add_filter('page_template_hierarchy', 'change_template_path');
add_filter('paged_template_hierarchy', 'change_template_path');
add_filter('privacypolicy_template_hierarchy', 'change_template_path');
add_filter('search_template_hierarchy', 'change_template_path');
add_filter('single_template_hierarchy', 'change_template_path');
add_filter('singular_template_hierarchy', 'change_template_path');
add_filter('tag_template_hierarchy', 'change_template_path');
add_filter('taxonomy_template_hierarchy', 'change_template_path');
승인된 답변을 코드 조각으로 마무리합니다.
<?php
/* Pages templates in sub folders */
/* --------------------------------------------------------------------------------- */
add_filter('theme_page_templates', function($post_templates) {
$directories = glob(get_template_directory() . '/pages/*' , GLOB_ONLYDIR);
foreach ($directories as $dir) {
$templates = glob($dir.'/*.php');
foreach ($templates as $template) {
if (preg_match('|Template'.' '.'Name: (.*)$|mi', file_get_contents($template), $name)) {
$post_templates['/pages/'.basename($dir).'/'.basename($template)] = $name[1];
}
}
}
return $post_templates;
});
이것은 재귀 함수를 사용하여 강화될 수 있습니다.현재는 다음 항목만 추가합니다.
./pages/\*/\*.php
get_template_part는 WordPress TEMA의 일부로만 작동합니다.
WordPress 테마(wp-content/temes/ )를 작성하면 원하는 PHP 템플릿, CSS 리소스 등에 사용할 수 있습니다.
여기에서 WordPress 테마를 작성하고 모든 내용을 코덱스에 포함하는 방법을 확인할 수 있습니다.
각 다른 설계에 대해 템플리트 파일("페이지 템플리트" 디렉토리 내)을 작성해야 합니다.
de Backend(wp-admin)에서 액세스할 수 있는 템플릿 파일을 작성하려면 다음을 추가해야 합니다.
<?php
/*
Template Name: TEMPLATE-NAME
*/
?>
각 파일(파일)의 맨 위에 표시됩니다.그런 다음 각 파일 내에서 get_template_part() 함수를 사용하여 설계의 블록 또는 모듈을 추가할 수 있습니다.
언급URL : https://stackoverflow.com/questions/44261944/wordpress-template-files-in-subdirectorys
'source' 카테고리의 다른 글
| React의 Virtual DOM은 정확히 어떻게 빨라집니까? (0) | 2023.03.29 |
|---|---|
| 휴지 상태를 설정하는 방법.spring-boot에서 format_sql을 사용할 수 있습니까? (0) | 2023.03.29 |
| Ajax를 통해 여러 데이터 필드를 전송하려면 어떻게 해야 합니까? (0) | 2023.03.29 |
| ng-form을 사용한 각도 ng-repeat, 컨트롤러에서의 검증 접근 (0) | 2023.03.29 |
| JSX에서 문자열 일부를 태그로 바꿉니다. (0) | 2023.03.29 |