kummerer94 / corcel
用于从Wordpress数据库中收集数据的包装器(jgrossi/corcel的维护分支)
Requires
- php: >=5.3.0
- illuminate/database: *
Requires (Dev)
- phpunit/phpunit: 4.2.2
This package is not auto-updated.
Last update: 2024-09-28 18:35:46 UTC
README
此包扩展了 jgrossi/corcel,因为它允许您使用Laravel的数据库配置
Corcel\BaseModel::setConfiguredConnection('mysql_wp');
Corcel正在开发中,但它在工作 :D
--
Corcel是一个类集合,旨在使用更好的语法检索Wordpress数据库数据。它使用为Laravel框架开发的Eloquent ORM,但您可以在任何类型的PHP项目中使用Corcel。
这样,您可以将Wordpress用作后端,插入帖子、自定义类型等,而在前端可以使用您想要的任何东西,如Silex、Slim Framework、Laravel、Zend,甚至是纯PHP(为什么不呢?)。
安装
要安装Corcel,只需创建一个 composer.json 文件并添加
"require": {
"kummerer94/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许可证。