Skip to content

Instantly share code, notes, and snippets.

@graemebryson
Created February 7, 2020 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graemebryson/92f5da5873a5edf0057d3b93b5cc4fdb to your computer and use it in GitHub Desktop.
Save graemebryson/92f5da5873a5edf0057d3b93b5cc4fdb to your computer and use it in GitHub Desktop.
// Register 'video' custom post type
function register_post_type() {
$labels = array(
'name' => __( 'Videos', 'video-post-type' ),
'singular_name' => __( 'Video', 'video-post-type' ),
'add_new' => __( 'Add Video', 'video-post-type' ),
'add_new_item' => __( 'Add Video', 'video-post-type' ),
'edit_item' => __( 'Edit Video', 'video-post-type' ),
'new_item' => __( 'New Video', 'video-post-type' ),
'view_item' => __( 'View Video', 'video-post-type' ),
'search_items' => __( 'Search Videos', 'video-post-type' ),
'not_found' => __( 'No videos found', 'video-post-type' ),
'not_found_in_trash' => __( 'No videos in the trash', 'video-post-type' ),
);
$supports = array(
'title',
'editor',
'revisions',
'thumbnail',
);
$args = array(
'labels' => $labels,
'supports' => $supports,
'public' => true,
'capability_type' => 'post',
'menu_position' => 30,
'menu_icon' => 'dashicons-video-alt2',
'show_in_rest' => true,
'has_archive' => true,
'hierarchical' => true,
// Permalink rewrite function in 'functions/posts.php'
'rewrite' => array( 'slug' => 'about-us/video-center/%category%', 'with_front' => false )
);
$args = apply_filters( 'case_result_post_type_args', $args );
register_post_type( $this->post_type, $args );
}
// Register 'video-category' taxonomy
function register_taxonomy_category() {
$taxonomies = array(
// Video categories
array(
'slug' => 'video-category',
'single_name' => 'Video Category',
'plural_name' => 'Video Categories',
'post_type' => 'video',
'rewrite' => array( 'slug' => 'about-us/video-center', 'with_front' => false );
),
);
foreach( $taxonomies as $taxonomy ) {
$labels = array(
'name' => $taxonomy['plural_name'],
'singular_name' => $taxonomy['single_name'],
'search_items' => 'Search ' . $taxonomy['plural_name'],
'all_items' => 'All ' . $taxonomy['plural_name'],
'parent_item' => 'Parent ' . $taxonomy['single_name'],
'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
'edit_item' => 'Edit ' . $taxonomy['single_name'],
'update_item' => 'Update ' . $taxonomy['single_name'],
'add_new_item' => 'Add New ' . $taxonomy['single_name'],
'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name',
'menu_name' => $taxonomy['plural_name']
);
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
'show_in_rest' => true
));
}
}
// Re-write %category% placeholder
function rewrite_video_cpt_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'video' ){
$terms = wp_get_object_terms( $post->ID, 'video-category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'rewrite_video_cpt_permalinks', 1, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment