简体   繁体   English

自定义 php 文件目录在 Wordpress 与木材 Twig

[英]Custom php file directory in Wordpress with Timber Twig

I am using Timber with Wordpress (version 5.4.2).我正在使用带有 Wordpress(版本 5.4.2)的木材 I have installed Timber's starter theme as a boilerplate.我已经安装了 Timber 的启动主题作为样板。

Timber leverages the Wordpress template hierarchy allowing you to create a custom PHP file for a given route . Timber 利用 Wordpress 模板层次结构,允许您为给定路线创建自定义 PHP 文件

Page.php (default in Timber starter theme) Page.php(木材启动器主题中的默认值)

/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * To generate specific templates for your pages you can use:
 * /mytheme/templates/page-mypage.twig
 * (which will still route through this PHP file)
 * OR
 * /mytheme/page-mypage.php
 * **(in which case you'll want to duplicate this file and save to the above path)**
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since    Timber 0.1
 */

$context = Timber::context();

$timber_post = new Timber\Post();

$context['post'] = $timber_post;

Timber::render( [ 'page-' . $timber_post->post_name . '.twig', 'page.twig' ], $context );

As per the comments in page.php and the Timber docs, I can create a custom PHP file to load a template for a given page by create it in the root directory of the theme ( mytheme/my-custom-php-file.php )根据 page.php 和 Timber 文档中的评论,我可以创建一个自定义 PHP 文件,通过在主题的根目录中创建它来加载给定页面的模板( mytheme/my-custom-php-file.php )

But I will be creating a lot of custom PHP files for the project I'm working on - it would be pretty messy and hard to manage if I just drop them all into the root directory of the theme.但是我将为我正在处理的项目创建许多自定义 PHP 文件 - 如果我只是将它们全部放到主题的根目录中,那将非常混乱且难以管理。

I would instead like to place these files into their own directory mytheme/src/ .相反,我想将这些文件放入他们自己的目录mytheme/src/中。 ex.前任。 mytheme/src/my-custom-php-file.php . mytheme/src/my-custom-php-file.php

Currently, Timber/Wordpress will not recognize this file in this directory.目前,Timber/Wordpress 无法识别此目录中的此文件。

Where in Timber and/or Wordpress is the directory in which to look for pages' PHP files defined and how can I update this to indicate mytheme/src/ ?在 Timber 和/或 Wordpress 中的哪个目录是查找页面定义的 PHP 文件的目录,我如何更新它以指示mytheme/src/

This is possible, but a little differently than you might expect.这是可能的,但与您的预期略有不同。 You have to put all theme-related files to a subfolder , including functions.php and style.css .您必须将所有与主题相关的文件放到一个子文件夹中,包括functions.phpstyle.css WordPress recognizes themes in subfolders. WordPress 可识别子文件夹中的主题。

So here's what a possible structure could look like:因此,可能的结构如下所示:

.
└── wp-content/themes/mytheme/
    ├── theme/
    │   ├── functions.php
    │   ├── index.php
    │   ├── page.php
    │   ├── single.php
    │   └── style.css
    ├── vendor/
    ├── views/
    ├── .gitignore
    ├── composer.json
    ├── package.json
    └── README.md

We're putting the template files that WordPress needs into a theme subfolder.我们将 WordPress 需要的模板文件放入主题子文件夹中。 You can still put your views folder in the theme root, because Timber also looks for Twig templates in that folder.您仍然可以将视图文件夹放在主题根目录中,因为 Timber 还会在该文件夹中查找 Twig 模板。

In your functions.php file, you would then require your Composer dependencies from the parent folder:在您的functions.php文件中,您将需要父文件夹中的 Composer 依赖项:

require_once dirname( __DIR__ ) . '/vendor/autoload.php';

And there might be some other places where you would have to update paths.并且可能在其他一些地方您必须更新路径。

We're currently thinking about making this the default for the Timber Starter Theme (see this pull request ).我们目前正在考虑将其设为 Timber Starter 主题的默认设置(请参阅此拉取请求)。

Looking at the template_include docs for I think you might be able to do something like this:查看template_include文档,我认为您可能可以执行以下操作:

// functions.php
add_filter( 'template_include', function( $template ) {
    return 'src/' . $template;
}, 99 );

OR或者

// functions.php
add_filter( 'template_include', function( $template ) {
    if (// some condition) {
        return 'src/' . $template;
    }
    
    return $template;
}, 99 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM