Skip to content

Instantly share code, notes, and snippets.

@tomfinitely
Created July 11, 2014 14:11
Show Gist options
  • Save tomfinitely/0d5c4fa3319a9625dd5b to your computer and use it in GitHub Desktop.
Save tomfinitely/0d5c4fa3319a9625dd5b to your computer and use it in GitHub Desktop.
Faculty Custom Post Type for WordPress
<?php
/*
Plugin Name: Faculty Custom Post Type
Description: Adds custom post types for faculty members
Version: 0.1
License: GPL
Author: Oli Dale
Author URI: http://wplift.com
*/
add_action( 'init', 'register_cpt_faculty' );
function register_cpt_faculty() {
$labels = array(
'name' => _x( 'Faculty', 'faculty' ),
'singular_name' => _x( 'Faculty', 'faculty' ),
'add_new' => _x( 'Add New', 'faculty' ),
'add_new_item' => _x( 'Add New Faculty Member', 'faculty' ),
'edit_item' => _x( 'Edit Faculty Member', 'faculty' ),
'new_item' => _x( 'New Faculty Member', 'faculty' ),
'view_item' => _x( 'View Faculty Member', 'faculty' ),
'search_items' => _x( 'Search Faculty', 'faculty' ),
'not_found' => _x( 'No faculty found', 'faculty' ),
'not_found_in_trash' => _x( 'No faculty found in Trash', 'faculty' ),
'parent_item_colon' => _x( 'Parent Faculty:', 'faculty' ),
'menu_name' => _x( 'Faculty', 'faculty' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Faculty names and descriptions',
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'faculty', $args );
}
add_action( 'init', 'faculty_department_init' );
function faculty_department_init() {
register_taxonomy('department',array('faculty'), array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Department', 'taxonomy general name' ),
'singular_name' => _x( 'Department', 'taxonomy singular name' ),
'search_items' => __( 'Search Departments' ),
'all_items' => __( 'All Departments' ),
'parent_item' => __( 'Parent Department' ),
'parent_item_colon' => __( 'Parent Department:' ),
'edit_item' => __( 'Edit Department' ),
'update_item' => __( 'Update Department' ),
'add_new_item' => __( 'Add New Department' ),
'new_item_name' => __( 'New Department Name' ),
'menu_name' => __( 'Department' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'department', 'hierarchical' => 'true' ),
));
}
add_action( 'admin_head', 'faculty_custom_post_type_icon' );
function faculty_custom_post_type_icon() {
?>
<style>
/* Admin Menu - 16px */
#menu-posts-faculty .wp-menu-image {
background: url(<?php bloginfo( 'wpurl' ); ?>/wp-content/plugins/faculty-plugin/images/icon-adminmenu16-sprite.png) no-repeat 6px 6px !important;
}
#menu-posts-faculty:hover .wp-menu-image, #menu-posts-faculty.wp-has-current-submenu .wp-menu-image {
background-position: 6px -26px !important;
}
/* Post Screen - 32px */
.icon32-posts-faculty {
background: url(<?php bloginfo( 'wpurl' ); ?>/wp-content/plugins/faculty-plugin/images/icon-adminpage32.png) no-repeat left top !important;
}
@media
only screen and (-webkit-min-device-pixel-ratio: 1.5),
only screen and ( min--moz-device-pixel-ratio: 1.5),
only screen and ( -o-min-device-pixel-ratio: 3/2),
only screen and ( min-device-pixel-ratio: 1.5) {
/* Admin Menu - 16px @2x */
#menu-posts-faculty .wp-menu-image {
background-image: url('<?php bloginfo( 'wpurl' ); ?>/wp-content/plugins/Faculty-Plugin/images/[email protected]') !important;
-webkit-background-size: 16px 48px;
-moz-background-size: 16px 48px;
background-size: 16px 48px;
}
/* Post Screen - 32px @2x */
.icon32-posts-faculty {
background-image: url('<?php bloginfo( 'wpurl' ); ?>/wp-content/plugins/Faculty-Plugin/images/[email protected]') !important;
-webkit-background-size: 32px 32px;
-moz-background-size: 32px 32px;
background-size: 32px 32px;
}
}
</style>
<?php }
/**
* Faculty Template for Taxonomies
*
*/
/*add_filter( 'template_include', 'su_faculty_template' );
function su_faculty_template( $template ) {
if( is_tax( array( 'department' ) ) )
$template = get_query_template( 'archive-faculty' );
return $template;
}*/
/**
* Add 'page-attributes' to Faculty Post Type
*
* @param array $args, arguments passed to register_post_type
* @return array $args
*/
add_filter( 'facultyposttype_args', 'su_faculty_post_type_args' );
function su_faculty_post_type_args( $args ) {
$args['supports'][] = 'page-attributes';
return $args;
}
/**
* Sort faculty by menu order
*
*/
add_action( 'pre_get_posts', 'su_faculty_query' );
function su_faculty_query( $query ) {
if( $query->is_main_query() && !is_admin() && ( is_post_type_archive( 'faculty' ) || is_tax( array( 'department' ) ) ) ) {
$query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment