Skip to content

Instantly share code, notes, and snippets.

@jsoningram
Created September 21, 2015 19:07
Show Gist options
  • Save jsoningram/e3d8d7d11ccaa32fa0f8 to your computer and use it in GitHub Desktop.
Save jsoningram/e3d8d7d11ccaa32fa0f8 to your computer and use it in GitHub Desktop.
Template for Custom Post Types
<?php
$post_type = 'Tours';
$singular = 'Tour';
$taxonomy = 'Adventures';
$singulartax = 'Adventure';
$for_post_type = $post_type;
add_action( 'init', 'cptui_register_my_cpts' );
function cptui_register_my_cpts() {
global $taxonomy, $singular, $for_post_type, $post_type, $singulartax;
$labels = array(
"name" => $post_type,
"singular_name" => $singular,
"menu_name" => $post_type,
"all_items" => "All " . $post_type,
"add_new" => "Add New",
"add_new_item" => "Add New " . $singular,
"edit" => "Edit",
"edit_item" => "Edit " . $singular,
"new_item" => "New " . $singular,
"view" => "View",
"view_item" => "View " . $singular,
"search_items" => "Search " . $singular,
"not_found" => "No " . $post_type . " found",
"not_found_in_trash" => "No " . $post_type . " found in Trash",
"parent" => "Parent " . $singular,
);
$args = array(
"labels" => $labels,
"description" => $post_type,
"public" => true,
"show_ui" => true,
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => strtolower($singular), "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor", "excerpt", "custom-fields", "revisions", "thumbnail", "page-attributes" ),
);
register_post_type( strtolower($singular), $args );
// End of cptui_register_my_cpts()
}
add_action( 'init', 'cptui_register_my_taxes' );
function cptui_register_my_taxes() {
global $taxonomy, $singular, $for_post_type, $post_type, $singulartax;
$labels = array(
"name" => $taxonomy,
"label" => $taxonomy,
"menu_name" => $taxonomy,
"all_items" => "All " . $taxonomy,
"edit_item" => "Edit " . $singulartax,
"view_item" => "View " . $singulartax,
"update_item" => "Update " . $singulartax . " name",
"add_new_item" => "Add New " . $singulartax,
"new_item_name" => "New " . $singulartax . " Name",
"parent_item" => "Parent " . $singulartax,
"parent_item_colon" => "Parent " . $singulartax . ":",
"search_items" => "Search " . $taxonomy,
"popular_items" => "Popular " . $taxonomy,
"separate_items_with_commas" => "Separate " . $singulartax . " with commas",
"add_or_remove_items" => "Add or remove " . $taxonomy,
"choose_from_most_used" => "Choose from the most used " . $taxonomy,
"not_found" => "No " . $singulartax . " found",
);
$args = array(
"labels" => $labels,
"hierarchical" => true,
"label" => $taxonomy,
"show_ui" => true,
"query_var" => true,
"rewrite" => array( 'slug' => strtolower($singulartax), 'with_front' => true ),
"show_admin_column" => true,
);
register_taxonomy( strtolower($singulartax), array( strtolower($singular) ), $args );
// End cptui_register_my_taxes
}
function filter_tours_link($link, $post) {
global $taxonomy, $singular, $for_post_type, $post_type, $singulartax;
if ($post->post_type != strtolower($post_type)) :
return $link;
endif;
if ($cats = get_the_terms($post->ID, strtolower($taxonomy))) :
$link = str_replace('%' . strtolower($taxonomy) . '%', array_pop($cats)->slug, $link);
return $link;
endif;
} add_filter('post_type_link', 'filter_tours_link', 10, 2);
// if ( ! function_exists( 'unregister_post_type' ) ) :
// function unregister_post_type( $post_type ) {
// global $wp_post_types, $post_type, $singular;
// if ( isset( $wp_post_types[ $post_type ] ) ) {
// unset( $wp_post_types[ $post_type ] );
// return true;
// }
// return false;
// }
// endif;
// flush_rewrite_rules();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment