如何将页面模板移动到自定义文件夹?

时间:2016-05-18 作者:Den

例如,我创建了使用页面slug的页面模板,page-contact.php, page-gallery.php 等等或页面id,例如,page-2.php, page-11.php

例如,如何将这些模板移动到子文件夹/mytheme/pages/?

3 个回复
SO网友:Andy Macaulay-Brook

/您的主题/页面模板/仅适用于在管理页面编辑屏幕上分配的自定义页面模板,而不适用于页面-$slug或页面-$id命名模板。

在我的视图中,正确的过滤器挂钩是page\\u template,但您没有(我假设!)想为你的页面扔掉任何其他可能的模板,尤其是因为你的网站上一定会有一些页面,而你还没有为这些页面创建一个/my sub dir/page-slug。php模板文件。

在WP使用标准模板层次结构找到页面的模板后,立即调用page\\u template筛选器挂钩。如果有一个过滤器可以让您将额外的模板注入模板层次结构的正确部分,这将非常方便,但如果没有过滤器,我们需要从WordPress自己的get\\u page\\u template()函数中复制对页面模板的搜索,该函数位于/wp includes/template中。php:

function get_page_template() {
    $id = get_queried_object_id();
    $template = get_page_template_slug();
    $pagename = get_query_var(\'pagename\');

    if ( ! $pagename && $id ) {
        // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
        $post = get_queried_object();
        if ( $post )
            $pagename = $post->post_name;
    }

    $templates = array();
    if ( $template && 0 === validate_file( $template ) )
        $templates[] = $template;
    if ( $pagename )
        $templates[] = "page-$pagename.php";
    if ( $id )
        $templates[] = "page-$id.php";
    $templates[] = \'page.php\';

    return get_query_template( \'page\', $templates );
}
此函数用于为页面构建一组可能的模板。get\\u query\\u template()然后使用locate\\u template()遍历数组并返回找到的第一个模板的文件名。

由于我们无法连接到提议的模板列表中,很遗憾,我们将不得不重复一些这项工作。

以下是我们自己的功能:

function tbdn_get_page_template() {
    $id = get_queried_object_id();
    $template = get_page_template_slug();
    $pagename = get_query_var(\'pagename\');

    if ( ! $pagename && $id ) {
        // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
        $post = get_queried_object();
        if ( $post )
            $pagename = $post->post_name;
    }

    $templates = array();

    if ( $template && 0 === validate_file( $template ) )
        $templates[] = $template;
    // if there\'s a custom template then still give that priority

    if ( $pagename )
        $templates[] = "our-sub-dir/page-$pagename.php";
    // change the default search for the page-$slug template to use our directory
    // you could also look in the theme root directory either before or after this

    if ( $id )
        $templates[] = "our-sub-dir/page-$id.php";
    $templates[] = \'page.php\';

    /* Don\'t call get_query_template again!!!
       // return get_query_template( \'page\', $templates );
       We also reproduce the key code of get_query_template() - we don\'t want to call it or we\'ll get stuck in a loop .
       We can remove lines of code that we know won\'t apply for pages, leaving us with...
    */

    $template = locate_template( $templates );

    return $template;
}

add_filter( \'page_template\', \'tbdn_get_page_template\' );
注意事项:

1-我还没有对此进行测试,但如果你想搞乱模板层次结构,那么你肯定能够遵循,测试&;调整我的代码,这主要是复制自WP无论如何。

2-如果将来的核心代码更改了页面的模板层次结构,那么上面的代码将过时。

SO网友:Landing on Jupiter

WordPress将识别/YOUR\\u THEME/page templates/中的模板文件:https://developer.wordpress.org/themes/basics/organizing-theme-files/#page-templates-folder

SO网友:Fayaz

我将详细信息发布在this answer. 在此,根据该问题的两个具体差异调整代码:

将页面模板移动到子目录而不后退。例如,它将检查THEME/sub-directory/page-{slug}.php, 但它会not 检查THEME/page-{slug}.php, 因为这就是OP的要求。但是,在另一个答案中使用回退的选项更好,尤其是在子主题的情况下(因为父主题可能取决于回退)。

它会同时移动page-{slug}.phppage-{id}.php 到子目录。

要使用的过滤器挂钩:将页面模板移动到子目录的最佳方法,例如/THEME/page-templates/, 是使用page_template_hierarchy 过滤器挂钩。

Note:原始过滤器挂钩为{$type}_template_hierarchy, 成为page_template_hierarchy 什么时候$typepage.

示例代码:

<?php
/*
Plugin Name:  WPSE Page Template move to Sub Directory
Plugin URI:   https://wordpress.stackexchange.com/a/227006/110572
Description:  WPSE Page Template move to a Sub Directory
Version:      1.0.0
Author:       Fayaz Ahmed
Author URI:   https://www.fayazmiraz.com/
*/

// defining the sub-directory so that it can be easily accessed from elsewhere as well.
define( \'WPSE_PAGE_TEMPLATE_SUB_DIR\', \'page-templates\' );

function wpse227006_page_template_add_subdir( $templates = array() ) {
    // Generally this doesn\'t happen, unless another plugin / theme does modifications
    // of their own. In that case, it\'s better not to mess with it again with our code.
    if( empty( $templates ) || ! is_array( $templates ) || count( $templates ) < 3 )
        return $templates;

    $page_tpl_idx = 0;
    $cnt = count( $templates );
    if( $templates[0] === get_page_template_slug() ) {
        // if there is custom template, then our page-{slug}.php template is at the next index 
        $page_tpl_idx = 1;
    }

    // the last one in $templates is page.php, so
    // all but the last one in $templates starting from $page_tpl_idx will be moved to sub-directory
    for( $i = $page_tpl_idx; $i < $cnt - 1; $i++ ) {
        $templates[$i] = WPSE_PAGE_TEMPLATE_SUB_DIR . \'/\' . $templates[$i];
    }

    return $templates;
}
// the original filter hook is {$type}_template_hierarchy,
// wihch is located in wp-includes/template.php file
add_filter( \'page_template_hierarchy\', \'wpse227006_page_template_add_subdir\' );

相关推荐

Updating modified templates

我想调整一些。php模板文件在我的WordPress主题中,我知道正确的过程是将相关文件复制到子主题文件夹中并在那里编辑文件,这样我的修改就不会在将来的主题更新中丢失。但这是否意味着我将无法从主题更新中获益,因为我将文件放在了我的子主题文件夹中?这可能不是一件好事,因为主题更新可能添加了一些有用的特性,甚至修复了我最初需要对代码进行调整的问题!这方面的常见解决方案是什么?有人推荐了一款Diff应用程序——这是人们常用的吗?