-
Notifications
You must be signed in to change notification settings - Fork 5
Model
Models in Alter are named by convetion over configuration. Before creating a model, you need to create a folder named model in your theme folder root, all model files must me created into this directory.
Models are directly linked to Wordpress Post Types. So, if you create a model called BookModel
, Alter will register the Book
Post Type.
To create a model is very simple, you need to use this convention to name the class: <Entity name>Model
;
See the example:
<?php
class CarModel extends AppModel{
}
To set fields in your model just create a instance variable called fields
with the fields that you want in that post type.
<?php
class CarModel extends AppModel{
public $fields = array(
'editor' => true,
'title' => true,
'thumbmail' => true
);
}
Alter uses the rw-meta-box <https://github.com/rilwis/meta-box>
_ to create custom fields in your post type. To set your custom fields in the model you need to specify the label and the type of the field:
<?php
class CarModel extends AppModel{
public $fields = array(
// Default Wordpress Fields
'editor' => true,
'title' => true,
'thumbmail' => true ,
// Custom Fields
'manufacturer' => array(
'label' => 'Manufacturer',
'type' => 'text'
)
);
}
The types available are:
- text
- long_text
- int
- float
- boolean
- image
- file
To set that the custom field may has more than one item you can set the multiple parameter to true:
<?php
class CarModel extends AppModel{
public $fields = array(
// Default Wordpress Fields
'editor' => true,
'title' => true,
'thumbmail' => true ,
// Custom Fields
'manufacturer' => array(
'label' => 'Manufacturer',
'type' => 'text'
),
'photo' => array(
'label' => 'Photo Gallery',
'type' => 'image',
'multiple' => true
)
);
}
Route is the path that your posts of a model will be accessible to the public. In the example above, my posts of type book will be accessible in http://mysite.com/books:
class CarModel extends AppModel{
public $route = 'books';
You can setup the labels with two attributes of a model object: singular and plural. You can also specify a icon for the Post Type in the Wordpress Admin
class CarModel extends AppModel{
public $singular = "Car";
public $plural = "Cars";
public $icon = "dashicons-admin-home";
You can easily link a model to one or more taxonomies, see:
class CarModel extends AppModel{
public $taxonomies = array('car_type', 'car_color');