johnbillion/extended-template-parts

这是一个库,为WordPress模板部分提供扩展功能,包括模板变量和缓存。

2.2.1 2023-05-09 23:55 UTC

This package is auto-updated.

Last update: 2024-08-28 02:49:40 UTC


README

Stable Release License PHP 7

扩展模板部分

扩展模板部分是一个库,为WordPress模板部分提供扩展功能,包括模板变量和片段缓存。

特性

  • 将变量传递到模板部分并通过 $this->vars 数组访问它们。不会污染全局变量!
  • 使用transients轻松缓存模板部分。

最低要求

PHP 7.0
WordPress 4.4

安装

扩展模板部分是一个开发库,而不是插件,这意味着您需要在自己的项目中某处包含它。您可以使用Composer

composer require johnbillion/extended-template-parts

或者您可以直接下载库并将其手动包含

require_once 'extended-template-parts/extended-template-parts.php';

基本用法

get_extended_template_part() 函数的行为与WordPress的 get_template_part() 函数 完全一样,只不过它从主题的 template-parts 子目录中加载模板部分,以获得更好的文件组织。遵循通常的父/子主题层次结构。

get_extended_template_part( 'foo', 'bar' );

使用 $vars 参数将变量数组传递到模板部分

get_extended_template_part( 'foo', 'bar', [
	'my_variable' => 'Hello, world!',
] );

在您的 template-parts/foo-bar.php 模板部分文件中,您可以通过使用 $this->vars 访问传递的变量

echo esc_html( $this->vars['my_variable'] );

高级用法

get_extended_template_part() 函数还接受一个可选的第二个参数,用于控制目录名称和缓存。

以下代码将从 my-directory 子目录加载 foo-bar.php 并将其输出自动缓存为一个transient,持续一小时

get_extended_template_part( 'foo', 'bar', [
	'my_variable' => 'Hello, world!',
], [
	'dir'   => 'my-directory',
	'cache' => 1 * HOUR_IN_SECONDS,
] );

许可证:GPLv2或更高版本

本程序是自由软件;您可以在自由软件基金会发布的GNU通用公共许可证的条款下重新分发和/或修改它;许可证的第2版,或者(根据您的选择)许可证的任何更高版本。

本程序的分发是希望它有用,但没有任何保证;甚至没有任何暗示的保证,即其适销性或适用于特定目的。有关详细信息,请参阅GNU通用公共许可证。