ycms / corcel
用于从WordPress数据库收集数据的包装器
dev-master
2015-07-15 09:05 UTC
Requires
- php: >=5.3.0
- illuminate/database: *
Requires (Dev)
- phpunit/phpunit: 4.2.2
Replaces
- juniorgrossi/corcel: dev-master
This package is not auto-updated.
Last update: 2024-09-14 16:39:54 UTC
README
Corcel正在开发中,但它在工作:D
--
Corcel是一个用于通过更好的语法检索WordPress数据库数据的类集合。它使用为Laravel框架开发的Eloquent ORM,但你可以在任何类型的PHP项目中使用Corcel。
这样,你可以将WordPress作为后端,用于插入帖子、自定义类型等,你可以在前端使用任何你想要的东西,比如Silex、Slim Framework、Laravel、Zend,甚至是纯PHP(为什么不呢?)。
安装
要安装Corcel,只需创建一个composer.json
文件并添加以下内容:
"require": {
"jgrossi/corcel": "dev-master"
},
之后,运行composer install
并等待。
使用方法
首先,你必须包含Composer的autoload
文件。
require __DIR__ . '/vendor/autoload.php';
现在,你必须设置你的WordPress数据库参数
$params = array(
'database' => 'database_name',
'username' => 'username',
'password' => 'pa$$word',
'prefix' => 'wp_' // default prefix is 'wp_', you can change to your own prefix
);
Corcel\Database::connect($params);
你可以指定所有的Eloquent参数,但一些是默认的(但你可以覆盖它们)。
'driver' => 'mysql',
'host' => 'localhost',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'wp_', // Specify the prefix for wordpress tables, default prefix is 'wp_'
帖子
// 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);
待办事项
我正在处理WordPress评论集成。
许可证
Corcel受MIT许可证的许可。