rajivseelam/corcel

用于从 WordPress 数据库中收集数据的包装器

dev-add_tags_categories_to_post 2014-06-27 08:23 UTC

This package is auto-updated.

Last update: 2024-09-15 18:37:18 UTC


README

此软件包可帮助您将 WordPress 与您的 Laravel 安装集成。

假设您将 WordPress 放在 public/wordpress 目录下,并通过 http://xyz.com/wordpress 打开 WordPress 博客。但是,也许出于某种疯狂的原因,您想使用 Laravel 的路由和控制器来展示博客,比如在 http://xyz.com/blog!这样您就可以控制外观,将博客添加到任何其他页面,同时使用 WordPress 管理员访问该数据库。

当然,您可以使用原始的数据库语句来完成这项工作!但这有什么乐趣呢?Corcel 为您提供了 Eloquent 类来完成这些操作!

Corcel 正在开发中。

感谢 Junior Grossi 开始这个项目!

我扩展了一些功能。

--

Corcel 是一个用于通过更好的语法检索 WordPress 数据库数据的类集合。它使用为 Laravel 框架开发的 Eloquent ORM,但您可以在任何类型的 PHP 项目中使用 Corcel。

这样,您可以使用 WordPress 作为后端,插入文章、自定义类型等,而在前端可以使用您想要的任何东西,比如 Silex、Slim 框架、Laravel、Zend,甚至是纯 PHP(为什么不呢?)。

安装

要安装 Corcel,只需创建一个 composer.json 文件并添加以下内容

"require": {
    "rajivseelam/corcel": "dev-master"
},

在您的 public/index.php 中包含以下内容(以便我们使用 WP 函数)

define('WP_USE_THEMES', false);
require __DIR__.'/wordpress/wp-blog-header.php';

然后运行 composer install 并等待。

连接到数据库

如果您正在使用与 Laravel 一起的 WP,则不需要这样做。

首先,您必须包含 Composer 的 autoload 文件。

require __DIR__ . '/vendor/autoload.php';

现在,您必须设置您的 WordPress 数据库参数

$params = array(
    'database'  => 'database_name',
    'username'  => 'username',
    'password'  => 'pa$$word',
);
Corcel\Database::connect($params);

您可以指定所有 Eloquent 参数,但一些是默认的(但您可以覆盖它们)。

'driver'    => 'mysql',
'host'      => 'localhost',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',

用法

文章

// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// A specific post
$post = Corcel\Post::find(31);
echo $post->post_title;

// A specific post by slug
$post = Corcel\Post::slug($slug)->first();

您还可以从文章中检索元数据。

// Get a custom meta value (like 'link' or whatever) from a post (any type)
$post = Corcel\Post::find(31);
echo $post->meta->link; // OR
echo $post->link;

//Categories or Tags of a post
$post->categories(); 

$post->tags();

//Get thumbnail url
$post->thumbnail_url();

//Get the large version
$post->thumbnail_url('large');

//Find posts under a category

$category = Corcel\Category::where('slug',$slug)->first();

$posts = $category->posts();

    
//Find posts under a tag

$tag = Corcel\Tag::where('slug',$slug)->first();

$posts = $tag->posts();

分类和标签

// Get all categories

$categories = Corcel\Category::all();

自定义文章类型

您还可以处理自定义文章类型。您可以使用 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;
}

页面

页面类似于自定义文章类型。您可以使用 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;

更多

查看代码以获取更多信息!

许可证

Corcel 在 MIT 许可下授权。