Skip to content
Sérgio Vilar edited this page Dec 5, 2014 · 9 revisions

Models

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.

Creating Models

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{

	}

Setting Model fields

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     
	    );

	}

Custom fields

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

Multiple custom fields

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
	        )

	    );

	}

Setting up a route

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';

Labels and icon for the Wordpress Admin

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";

Linking a model to a taxonomy

You can easily link a model to one or more taxonomies, see:

class CarModel extends AppModel{

	public $taxonomies = array('car_type', 'car_color');