The Easiest Way to Save and Share Code Snippets on the web

Modified Multiple Custom Post Types

php

posted: Nov, 18th 2011 | jump to bottom

/* Add Post Type */
	function add_post_type($name, $args = array() ) {	
		if ( !isset($name) ) return;
 
		$name = strtolower(str_replace(' ', '_', $name));
 
		add_action('init', function() use($name, $args) {	
			$args = array_merge(
				array(
					'label' => 'All ' . ucwords($name) . 's',
					'labels' => array('add_new_item' => "Add New $name"),
					'singular_name' => $name,
					'public' => true,
					'supports' => array('title', 'editor', 'comments'),
				),
				$args
			);
 
			register_post_type( $name, $args);
		});
	}
 
 
	/* Add Taxonomy */
	function add_taxonomy($taxonomy_name, $post_type, $args = array() ) {
		$taxonomy_name = strtolower($taxonomy_name);
 
		add_action('init', function() use($taxonomy_name, $post_type, $args) {
			// name of taxonomy, associated post type, options
			register_taxonomy($taxonomy_name, $post_type, array(
				'label' => ucwords($taxonomy_name),
			));
		});
	}
 
//--[book CPT]--------------------------------------------------
	add_post_type('book', array(
		'supports' => array('title', 'editor', 'thumbnail', 'comments')
	));
 
 
	add_taxonomy('software', 'book');
	add_taxonomy('novel', 'book');	
 
 
 
	//--[movie cpt]--------------------------------------------------
 
 
 
 
	add_post_type('movie', array(
		'supports' => array('title', 'editor', 'thumbnail', 'comments')
	));
 
	/* Add Taxonomy */
		add_taxonomy('Actor', 'movie');
	add_taxonomy('Director', 'movie');
250 views