Skip to content

Instantly share code, notes, and snippets.

@bmakowski
Created October 25, 2016 11:21
Show Gist options
  • Save bmakowski/a52ebaca4b24fef4db0b3569bf311007 to your computer and use it in GitHub Desktop.
Save bmakowski/a52ebaca4b24fef4db0b3569bf311007 to your computer and use it in GitHub Desktop.
WordPress Custom Post Types Examples
<?php
/*
Example 1 - custom post type without category
Author: Bartek Makowski
*/
//inside wp-content/themes/{template}/functions.php
function create_post_type() {
register_post_type( 'careers',
array(
'labels' => array(
'name' => __( 'Careers' ),
'singular_name' => __( 'Career' )
),
'public' => true,
'has_archive' => true,
'menu_position' => 20,
'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
'rewrite' => array('slug' => 'careers'),
)
);
}
add_action( 'init', 'create_post_type' );
/*
Example 2 - custom post types with category, using custom taxonomies
Author: Bartek Makowski
*/
function create_post_type() {
register_post_type( 'menu',
array(
'labels' => array(
'name' => __( 'Menu' ),
'singular_name' => __( 'Menu' )
),
'public' => true,
'has_archive' => true,
'menu_position' => 20,
'supports' => array('title', 'thumbnail', 'revisions'),
'taxonomies'=>array('menu_category'),
'rewrite' => array('slug' => 'menu'),
)
);
}
add_action( 'init', 'create_post_type' );
function create_menu_tax() {
register_taxonomy(
'menu_category',
'menu',
array(
'label' => __( 'Menu Category' ),
'rewrite' => array( 'slug' => 'menu_category' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_menu_tax' );
//For more info refer to https://codex.wordpress.org/Post_Types#Custom_Post_Types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment