Created
February 6, 2014 03:15
-
-
Save Kenshino/8837888 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// domain.com/videos/funny/cute-dogs/ | |
function add_video_post_type() { | |
$labels = array( | |
'name' => _x( 'Videos', 'Post Type General Name', 'kibble' ), | |
'singular_name' => _x( 'Video', 'Post Type Singular Name', 'kibble' ), | |
'menu_name' => __( 'Videos', 'kibble' ), | |
'parent_item_colon' => __( 'Parent Item:', 'kibble' ), | |
'all_items' => __( 'All Items', 'kibble' ), | |
'view_item' => __( 'View Item', 'kibble' ), | |
'add_new_item' => __( 'Add New Item', 'kibble' ), | |
'add_new' => __( 'Add New', 'kibble' ), | |
'edit_item' => __( 'Edit Item', 'kibble' ), | |
'update_item' => __( 'Update Item', 'kibble' ), | |
'search_items' => __( 'Search Item', 'kibble' ), | |
'not_found' => __( 'Not found', 'kibble' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'kibble' ), | |
); | |
$rewrite = array( | |
'slug' => 'videos/%video_types%', | |
'with_front' => true, | |
'pages' => true, | |
'feeds' => true, | |
); | |
$args = array( | |
'label' => __( 'video_type', 'kibble' ), | |
'description' => __( 'Video Posts', 'kibble' ), | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'thumbnail', ), | |
'taxonomies' => array( 'video_types' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-format-video', | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'rewrite' => $rewrite, | |
'capability_type' => 'page', | |
); | |
register_post_type( 'video_type', $args ); | |
} | |
add_action( 'init', 'add_video_post_type', 0 ); | |
function custom_video_types() { | |
$labels = array( | |
'name' => _x( 'Video Categories', 'Taxonomy General Name', 'kibble' ), | |
'singular_name' => _x( 'Video Category', 'Taxonomy Singular Name', 'kibble' ), | |
'menu_name' => __( 'Categories', 'kibble' ), | |
'all_items' => __( 'All Items', 'kibble' ), | |
'parent_item' => __( 'Parent Item', 'kibble' ), | |
'parent_item_colon' => __( 'Parent Item:', 'kibble' ), | |
'new_item_name' => __( 'New Item Name', 'kibble' ), | |
'add_new_item' => __( 'Add New Item', 'kibble' ), | |
'edit_item' => __( 'Edit Item', 'kibble' ), | |
'update_item' => __( 'Update Item', 'kibble' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'kibble' ), | |
'search_items' => __( 'Search Items', 'kibble' ), | |
'add_or_remove_items' => __( 'Add or remove items', 'kibble' ), | |
'choose_from_most_used' => __( 'Choose from the most used items', 'kibble' ), | |
'not_found' => __( 'Not Found', 'kibble' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
); | |
register_taxonomy( 'video_types', 'video_type', $args ); | |
} | |
add_action( 'init', 'custom_video_types', 0 ); | |
add_filter('post_type_link', 'filter_post_type_link', 10, 2); | |
function filter_post_type_link( $link, $post ) { | |
if ( strpos( $link, '%video_types%' ) === false ) | |
return $link; | |
$terms = wp_get_object_terms( $post->ID, 'video_types' ); | |
if ( current( $terms ) ) : | |
$new_parent = current( $terms ); | |
foreach ( $terms as $term ) { | |
if ( $term->parent == 0 ) { | |
$new_parent = $term; | |
break; | |
} | |
} | |
$new_parent = $new_parent ? $new_parent : $terms[0]; | |
$category_string = $new_parent->slug; | |
$category_string = trim( $category_string, '/' ); | |
endif; | |
// set a default | |
$category_string = ( isset( $category_string ) && $category_string ) ? $category_string : 'uncategorized'; | |
$link = str_replace( '%video_types%', $category_string, $link ); | |
$link = str_replace( '%post_name%', $post->post_name, $link ); | |
return $link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment