Skip to content

Instantly share code, notes, and snippets.

@webhasan
Last active August 29, 2015 14:13
Show Gist options
  • Save webhasan/21ef6dbca686b09c947c to your computer and use it in GitHub Desktop.
Save webhasan/21ef6dbca686b09c947c to your computer and use it in GitHub Desktop.
WordPress Custom Taxonomy
<?php
/**
* The core plugin class file
*
* Defines the functions necessary to register our custom taxonomies with
* WordPress.
*
* @link http://code.tutsplus.com/series/the-beginners-guide-to-wordpress-taxonomies--cms-706
* @since 1.0.0
*
* @package Custom_Taxonomies
* @author Tom McFarlin <[email protected]>
*/
class My_Custom_Taxonomies {
public function __construct(){
add_action( 'init', array( $this, 'init_photographs' ) );
add_action( 'init', array( $this, 'init_videos' ) );
add_action( 'init', array( $this, 'init_film_type' ) );
}
/**
* Initializes the plugin by registering the hooks necessary
* for creating our custom taxonomies within WordPress.
*
* @since 1.0.0
*/
/**
* Creates the Photographs taxonomy that appears on all Post dashboard
* pages.
*
* @since 1.0.0
*/
public function init_photographs(){
$labels = array(
'name' => 'Photographs',
'singular_name' => 'Photograph',
'edit_item' => 'Edit Photograph',
'update_item' => 'Update Photograph',
'add_new_item' => 'Add New Photograph',
'menu_name' => 'Photographs'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'photograph' )
);
register_taxonomy('photograph','post', $args);
}
/**
* Creates the Videos taxonomy that appears on all Post dashboard
* pages.
*
* @since 1.0.0
*/
public function init_videos() {
$labels = array(
'name' => 'Videos',
'singular_name' => 'Video',
'edit_item' => 'Edit Video',
'update_item' => 'Update Video',
'add_new_item' => 'Add New Video',
'menu_name' => 'Videos'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'video' )
);
register_taxonomy( 'video', 'post', $args );
}
/**
* Creates the Film Type taxonomy that appears on all Post dashboard
* pages.
*
* @since 1.0.0
*/
public function init_film_type() {
$labels = array(
'name' => 'Film Type',
'singular_name' => 'Film Type',
'edit_item' => 'Edit Film Type',
'update_item' => 'Update Film Type',
'add_new_item' => 'Add New Film Type',
'menu_name' => 'Film Type'
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array( 'slug' => 'film-type' )
);
register_taxonomy( 'film-type', 'post', $args );
}
}
<?php
/**
* My Custom Taxonomies
*
* Demonstrates how to create custom taxonomies using the WordPress API.
* Showcases both hierarchical and non-hierarchical taxonomies.
*
* @link http://code.tutsplus.com/series/the-beginners-guide-to-wordpress-taxonomies--cms-706
* @since 1.0.0
* @package Custom_Taxonomies
*
* @wordpress-plugin
* Plugin Name: My Custom Taxonomies
* Plugin URI: http://example.com/plugin-name-uri/
* Description: Demonstrates how to create custom taxonomies using the WordPress API.
* Version: 1.0.0
* Author: Tom McFarlin
* Author URI: http://tommcfarlin.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/** Loads the custom taxonomy class file. */
require_once( dirname( __FILE__ ) . '/class-my-custom-taxonomies.php' );
$custom_tax = new My_Custom_Taxonomies();
//use: the_terms( $post->ID, 'photograph', 'Photograph: ', ', ', ' ' );
// <?php the_terms( $id, $taxonomy, $before, $sep, $after ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment