redbeed / corcel-laravel5
此包已被废弃,不再维护。未建议替代包。
使用 Laravel 5 从 WordPress 数据库收集数据的包装器
dev-master
2015-08-25 19:42 UTC
Requires
- php: >=5.4.0
- illuminate/database: *
Requires (Dev)
- phpunit/phpunit: 4.2.2
This package is not auto-updated.
Last update: 2023-07-08 11:35:59 UTC
README
这是 jgrossi/corcel 的一个分支。
--
Corcel 是一个类集合,用于使用更好的语法检索 WordPress 数据库数据。它使用 Laravel 5 框架开发的 Eloquent ORM。
这样,您可以将 WordPress 作为后端使用,插入帖子、自定义类型等。
安装
要安装 Corcel,只需创建一个 composer.json
文件并添加
"require": {
"redbeed/corcel-laravel5": "dev-master"
},
之后运行 composer update
并等待。
使用
首先,您必须将服务提供者添加到 config/app.php
下的 providers
Corcel\CorcelServiceProvider::class
现在,您必须发布资源
php artisan vendor:publish
您可以在 corcel 配置文件中指定连接。
config/corcel.php
帖子
// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();
// A specific post
$post = Post::find(31);
echo $post->post_title;
您还可以从帖子中检索元数据。
// Get a custom meta value (like 'link' or whatever) from a post (any type)
$post = Post::find(31);
echo $post->meta->link; // OR
echo $post->fields->link;
echo $post->link; // OR
更新帖子自定义字段
$post = Post::find(1);
$post->meta->username = 'juniorgrossi';
$post->meta->url = 'http://grossi.io';
$post->save();
插入自定义字段
$post = new Post;
$post->save();
$post->meta->username = 'juniorgrossi';
$post->meta->url = 'http://grossi.io';
$post->save();
自定义帖子类型
您还可以处理自定义帖子类型。您可以使用 type(string)
方法或创建自己的类。
// using type() method
$videos = Post::type('video')->status('publish')->get();
// using your own class
class Video extends Corcel\Post
{
protected $postType = 'video';
}
$videos = Video::status('publish')->get();
自定义帖子类型和元数据
// Get 3 posts with custom post type (store) and show its title
$stores = Post::type('store')->status('publish')->take(3)->get();
foreach ($stores as $store) {
$storeAddress = $store->address; // option 1
$storeAddress = $store->meta->address; // option 2
$storeAddress = $store->fields->address; // option 3
}
分类法
您可以获取特定帖子的分类法,例如
$post = Post::find(1);
$taxonomy = $post->taxonomies()->first();
echo $taxonomy->taxonomy;
或者您可以使用其分类法搜索帖子
$post = Post::taxonomy('category', 'php')->first();
页面
页面类似于自定义帖子类型。您可以使用 Post::type('page')
或 Page
类。
// Find a page by slug
$page = Page::slug('about')->first(); // OR
$page = Post::type('page')->slug('about')->first();
echo $page->post_title;
分类 & 分类法
获取分类或分类法或从特定分类加载帖子。有多种方法可以实现。
// all categories
$cat = Taxonomy::category()->slug('uncategorized')->posts()->first();
echo "<pre>"; print_r($cat->name); echo "</pre>";
// only all categories and posts connected with it
$cat = Taxonomy::where('taxonomy', 'category')->with('posts')->get();
$cat->each(function($category) {
echo $category->name;
});
// clean and simple all posts from a category
$cat = Category::slug('uncategorized')->posts()->first();
$cat->posts->each(function($post) {
echo $post->post_title;
});
附件和修订版
从 Post
或 Page
获取附件和/或修订版。
$page = Page::slug('about')->with('attachment')->first();
// get feature image from page or post
print_r($page->attachment);
$post = Post::slug('test')->with('revision')->first();
// get all revisions from a post or page
print_r($post->revision);
许可
Corcel 在 MIT 许可下发布。