wp-xu / xu
v1.2.0
2016-01-18 17:40 UTC
Requires
- php: ^5.5.9 || ^7.0
- frozzare/tank: ~1.0
- frozzare/wp-autoload: ^2.0.0
Requires (Dev)
- frozzare/wp-test-suite: ~1.0
- wp-coding-standards/wpcs: dev-develop
README
需要 PHP 5.5.9 和 WordPress 4.4
WordPress 和 PHP 函数和类的集合。
安装
$ composer require frozzare/wp-xu
文档
一些缓存函数需要存在 wp_cache_delete_group
函数以删除缓存组。
模型
使用 xu,您可以创建从您的主题(使用 xu_get_model
)或任何地方加载的模型,只需使用类即可。
<?php use Xu\Model\Model; class Post extends Model { /** * Get model attributes. * * @return array */ public function get_attributes() { return [ 'title' => $this->post->post_title ]; } }
在您的模板文件中
$model = xu_get_model( 'post' ); echo $post->title;
模型类实现了许多方法,如 to_array
和 to_json
,以将模型转换为数组或 JSON 字符串。
模型集合
集合是包含一个或多个模型的集合。通过阅读API 参考文档,您可以了解集合有哪些方法,如 first
、last
、filter
、map
、reject
和 where
等,这些是其中一些方法。
集合可以以不同的方式创建
use Xu\Model\Collection; $collection = new Collection( [$model1, $model2] ); $collcetion = Post::collection( [$model1, $model2] ); class List extends Model { /** * Get all posts that exists on `post` post type. * * @return \Xu\Model\Collection */ public function posts() { return static::collection( get_posts( 'post_type=post' ) ); } } $collection = ( new List )->posts();
许可证
MIT © Fredrik Forsmo