Created
May 12, 2015 09:57
-
-
Save ChrisHursty/32c4485b6ec2088dd503 to your computer and use it in GitHub Desktop.
Custom Post Type, Taxonomy & Rewrite rules for slug
This file contains 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
/** | |
* create_news_post_type function. Create a News post type. | |
* | |
* @access public | |
* @return void | |
*/ | |
// POSTTYPES.PHP | |
function create_news_post_type() { | |
$labels = array( | |
'name' => _x( 'News', 'post type general name' ), | |
'singular_name' => _x( 'News', 'post type singular name' ), | |
'menu_name' => _x( 'News', 'admin menu' ), | |
'name_admin_bar' => _x( 'News', 'add new on admin bar' ), | |
'add_new' => _x( 'Add New', 'News' ), | |
'add_new_item' => __( 'Add New News' ), | |
'new_item' => __( 'New News' ), | |
'edit_item' => __( 'Edit News' ), | |
'view_item' => __( 'View News' ), | |
'all_items' => __( 'All News' ), | |
'search_items' => __( 'Search News' ), | |
'parent_item_colon' => __( 'Parent News:' ), | |
'not_found' => __( 'No news found.' ), | |
'not_found_in_trash' => __( 'No news found in Trash.' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'news' ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) | |
); | |
register_post_type( 'news', $args ); | |
} | |
add_action( 'init', 'create_news_post_type' ); | |
// POSTTYPES.PHP | |
// Next - Custom Taxonomy with Rewrite slug same as CPT | |
/** | |
* create_news_type_taxonomies function. Creates Type of News Taxonomy for taggin news. | |
* | |
* @access public | |
* @return void | |
*/ | |
function create_news_type_taxonomies() { | |
// Add new taxonomy, make it hierarchical (like categories) | |
$labels = array( | |
'name' => _x( 'Types of News', 'taxonomy general name' ), | |
'singular_name' => _x( 'Types of New', 'taxonomy singular name' ), | |
'search_items' => __( 'Search Type of News' ), | |
'all_items' => __( 'All Types of News' ), | |
'parent_item' => __( 'Parent Types of News' ), | |
'parent_item_colon' => __( 'Parent Types of News:' ), | |
'edit_item' => __( 'Edit Type of News' ), | |
'update_item' => __( 'Update Type of News' ), | |
'add_new_item' => __( 'Add New Type of News' ), | |
'new_item_name' => __( 'New Type of News Name' ), | |
'menu_name' => __( 'Type of News' ), | |
); | |
$args = array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'news' ), | |
); | |
register_taxonomy( 'news_type', array( 'news' ), $args ); | |
}//*/ | |
add_action( 'init', 'create_news_type_taxonomies', 0 ); | |
// FUNCTIONS.PHP | |
// New function that changes the way permalinks are saved in the post edit form so that news post will be saved with a url like www.mysite.com/news/new_type/post-slug. This function uses the post_type_link hook. | |
/** | |
* rewrite_news_post_links function. Adds rule to rewrite post permalinks when saved. | |
* | |
* @access public | |
* @param mixed $post_link | |
* @param int $id (default: 0) | |
* @return void | |
*/ | |
function rewrite_news_post_links( $post_link, $id = 0 ) { | |
$post = get_post($id); | |
// checks to see that there is a post and that it is a "news" post type. | |
if ( is_wp_error($post) || 'news' != $post->post_type || empty($post->post_name) ) | |
return $post_link; | |
// Get the news_type taxonomy associated with the post: | |
$terms = get_the_terms($post->ID, 'news_type'); | |
if( is_wp_error($terms) || !$terms ) { | |
//if you change the initial taxonomy to all you can replace 'uncategorize'd with "all" | |
$news_type = 'uncategorized'; | |
} | |
else { | |
$news_type_obj = array_pop($terms); | |
// if news post type has news_type taxonomy get the taxonomy slug | |
$news_type = $news_type_obj->slug; | |
} | |
// returns permalink in the form of /news/news-type-taxonomy-word/post-slug | |
return home_url(user_trailingslashit( "news/$news_type/$post->post_name" )); | |
} | |
add_filter( 'post_type_link', 'rewrite_news_post_links' , 10, 2 );//*/ | |
// FUNCTIONS.PHP | |
// rewrite rule that will allow wordpress to parse a url with the structure www.mysite.com/news/news_type/post-slug and return a post of the news post type with a matching taxonomy word and post slug. | |
/** | |
* rewrite_rules function. | |
* | |
* @access public | |
* @return void | |
*/ | |
function rewrite_rules() { | |
//adds rewrite rule so wordpress will recognize /news/anything/anything | |
add_rewrite_rule("^news/([^/]+)/([^/]+)/?",'index.php?post_type=news&news_type=$matches[1]&news=$matches[2]','top'); | |
} | |
add_action('init', 'rewrite_rules'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment