Getting Started

Requirements

Installation

Install with composer

Run the following in your terminal to install PostTypes with Composer.
1
$ composer require jjgrainger/posttypes
Copied!
PostTypes uses PSR-4 autoloading and can be used with the Composer's autoloader. Below is a basic example of getting started, though your setup may be different depending on how you are using Composer.
1
require __DIR__ . '/vendor/autoload.php';
2
3
use PostTypes\PostType;
4
5
$books = new PostType('book');
6
7
$books->register();
Copied!
See Composer's basic usage guide for details on working with Composer and autoloading.

Basic Usage

Below is a basic example of setting up a simple book post type with a genre taxonomy.
1
// Require the Composer autoloader.
2
require __DIR__ . '/vendor/autoload.php';
3
4
// Import PostTypes.
5
use PostTypes\PostType;
6
7
// Create a book post type.
8
$books = new PostType( 'book' );
9
10
// Attach the genre taxonomy (which is created below).
11
$books->taxonomy( 'genre' );
12
13
// Hide the date and author columns.
14
$books->columns()->hide( [ 'date', 'author' ] );
15
16
// Set the Books menu icon.
17
$books->icon( 'dashicons-book-alt' );
18
19
// Register the post type to WordPress.
20
$books->register();
21
22
// Create a genre taxonomy.
23
$genres = new Taxonomy( 'genre' );
24
25
// Set options for the taxonomy.
26
$genres->options( [
27
'hierarchical' => false,
28
] );
29
30
// Register the taxonomy to WordPress.
31
$genres->register();
Copied!
Last modified 1yr ago