redandblue / wordpress-tools
一套辅助WordPress开发的工具包。
dev-master
2022-06-02 09:43 UTC
Requires
- php: ^5.6 || ^7.0 || ^8.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-15 02:40:58 UTC
README
一套减少WordPress开发痛苦的工具包。存在文档,可能会随着时间的推移而改进。
它是模块化的,因此您可以禁用不需要的部分。
要求
PHP 7。
composer.json 说 5.6,但这只是临时的,所以Composer在安装标准macOS PHP(5.6)时不会抱怨,这是不合理的,因为升级非常困难。
有趣的部分
有很多事情在进行中,有些事情并不像其他事情那么有用。它给模板设计带来了新的气息。
首先,我们“注册”一个模板目录,将其添加到主题的 functions.php 中
\rnb\template\load_glob(dirname(__FILE__) . '/templates/*');
然后,来自 templates/ 的所有PHP文件都会通过 \rnb\template\output() 和 \rnb\template\to_string() 加载和可用。如果您想的话,可以拥有多个模板目录。
模板与“传统”模板略有不同:所有模板都是接受数据作为参数的函数,而传统的模板通常只是直接调用一个函数。例如,位于 templates/single-item.php 的模板
function single_item($props = []) { // We can also let the template populate itself with data if required. $content = $props['content'] ?? get_the_content(); $image = $props['image'] ?? \rnb\media\image(get_post_thumbnail_id()); $injectorfn = !is_null($data['injectorfn']) ? $data['injectorfn'] : function() { return '<!-- You can put just about anything here -->'; }; ?> <article> <?=$image?> <p><?=$content?></p> <?=$injectorfn()?> <!-- You could make the function echo data instead, choice is yours. --> </article> }
然后您可以在The Loop中使用该模板,或者以任何其他方式提供数据。
while (have_posts()) { the_post(); \rnb\template\output('single_item'); } // or \rnb\template\output('single_item', [ // notice array inside array [ 'content' => 'Hello world!', 'image' => '<img src="/path/to/image.jpg">', 'injectorfn' => function() { $link = get_permalink(); return \rnb\core\tag([ "<a href='$link'>", "Read the whole thing", "</a>" ]); } ] ]);
这一切都是在GitHub编辑器中编写的,实际上并没有测试代码,所以如果它能正常工作,那就太好了!如果它不能正常工作,请修复它并提交PR。