jozoor / wp-templater
WordPress 模块,用于通过插件添加自定义(页面和帖子类型)模板。
此软件包的官方仓库似乎已消失,因此软件包已被冻结。
v1.0.1
2019-12-04 08:14 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: 5.7
This package is not auto-updated.
Last update: 2024-01-31 05:32:47 UTC
README
WordPress 模块,用于通过插件添加自定义(页面和帖子类型)模板。
安装
❗ 最小要求
PHP 5.6
➖ 使用 Composer 安装
在您的终端中运行以下命令以使用 Composer 安装 wp-templater。
composer require jozoor/wp-templater
由于 wp-templater 使用 PSR-4 自动加载,您需要使用 Composer 的自动加载器。以下是一个基本示例,说明如何使用类,但根据您如何使用 Composer,您的设置可能不同,以下示例展示了创建新帖子类型的简单用法
require_once __DIR__ . '/vendor/autoload.php'; use JO\Module\Templater\Templater;
有关使用 Composer 和自动加载的详细信息,请参阅 Composer 的 基本用法 指南。
➖ 下载模块并手动包含
如果您不使用 Composer 并希望手动包含库,可以这样做,以下示例展示了创建新帖子类型的简单用法
/** * load main file */ require_once __DIR__ . '/wp-templater/src/Templater.php'; use JO\Module\Templater\Templater;
功能
在此模块/软件包中,我改进了由 PageTemplater 创建的 @source 类,由 wpexplorer 创建,我添加了以下功能
- 处理类方法和添加新功能。
- 现在您可以在类外定义您的 'Templater'
$settings和$templates。- @see 设置 Templater
- 您可以使用类外的过滤器覆盖最终自定义模板文件
- @see Templater.php
- 创建 composer 软件包。
- 支持任何帖子类型的自定义模板。
用法
use JO\Module\Templater\Templater; /** * How to use Templater */ // we should add our new Templater inside action hook add_action('plugins_loaded', 'load_templater'); function load_templater() { // setup our templater $my_templater = new Templater( array( // YOUR_PLUGIN_DIR or plugin_dir_path(__FILE__) 'plugin_directory' => plugin_dir_path(__FILE__), // should end with _ > prefix_ 'plugin_prefix' => 'plugin_prefix_', // templates directory inside your plugin 'plugin_template_directory' => 'templates', ) ); // add our new custom templates $my_templater->add( // array of available templates array( /** * default usage: * 'post_type_name' => array( * 'template_file.php' => 'template_name', * or * 'path/to/template_file.php' => 'template_name', * ), * * Note: all this files should be inside your * 'plugin_template_directory' => 'templates', * so this is parent directory > 'templates/path/template_file.php' */ // add 'post' type custom templates 'post' => array( // just file without any sub folders 'post-template.php' => 'Post Custom Template', // with sub folders 'path/to/post-template.php' => 'Post Custom Template', ), // add 'page' type custom templates 'page' => array( // just file without any sub folders 'page-template.php' => 'Page Custom Template', // with sub folders 'path/to/page-template.php' => 'Page Custom Template', ), // add 'custom_post_type' type custom templates, for ex: product 'product' => array( // just file without any sub folders 'product-template.php' => 'Product Custom Template', // with sub folders 'path/to/product-template.php' => 'Product Custom Template', ), // ..etc /** * Note: you can name your template file anything you like * i mean you shouldn't add post type name in template name, like * 'post-template.php' < this just for show you examples */ /** * Note: why we separated templates in the top by 'post types' ? * because we need this when working on any WP version 4.7 and later * which custom templates supported post types, but for WP version * 4.6 and older, all this templates will be merged, because we * working on 'page' enough, not like WP version 4.7 and later, * which we add templates only for exact post type. */ ) // here we actually will add all this new templates. )->register(); } /** * How to override final custom template file in themes or plugins */ add_filter('plugin_prefix_override_plugin_custom_template', 'override_plugin_custom_template'); function override_plugin_custom_template($template_file) { // add another template file here $template_file = plugin_dir_path(__FILE__) . 'templates/my_override_template.php'; // or do whatever .. // return new updated template or default template return $template_file; }
作者
Mohamed Abd Elhalim