Created
June 13, 2013 11:31
-
-
Save vajrasar/5773024 to your computer and use it in GitHub Desktop.
This is how I achieved following URL pattern while using the custom post type and custom taxonomy associated with it. - CPT Archive - xyz.com/classifieds - Custom Taxonomy - xyz.com/classifieds/automobiles - Single Post - xyz.com/classifieds/automobiles/Sell-My-Benz
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
<?php | |
/****Creating Custom Post Types of Classifieds Starts****/ | |
function my_custom_post_classifieds() { | |
$labels = array( | |
'name' => _x( 'Classifieds', 'post type general name' ), | |
'singular_name' => _x( 'Classified', 'post type singular name' ), | |
'add_new' => _x( 'Add New', 'book' ), | |
'add_new_item' => __( 'Add New Classified' ), | |
'edit_item' => __( 'Edit Classified' ), | |
'new_item' => __( 'New Classified' ), | |
'all_items' => __( 'All Classified' ), | |
'view_item' => __( 'View Classifieds' ), | |
'search_items' => __( 'Search Classifieds' ), | |
'not_found' => __( 'No Classifieds found' ), | |
'not_found_in_trash' => __( 'No Classifieds found in the Trash' ), | |
'parent_item_colon' => '', | |
'menu_name' => 'Classifieds' | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Holds all Classifieds kind of News', | |
'public' => true, | |
'menu_position' => 5, | |
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ), | |
'has_archive' => 'classifieds', | |
'rewrite' => array( 'slug' => 'classifieds/%classified_category%', 'with_front' => false ), | |
); | |
register_post_type( 'classifieds', $args ); | |
flush_rewrite_rules(); | |
} | |
add_action( 'init', 'my_custom_post_classifieds' ); | |
/****Creating Custom Post Types of Classifieds Ends****/ | |
/****Creating Custom Taxonomies for Classifieds *****/ | |
function my_taxonomies_classified_category() { | |
$labels = array( | |
'name' => _x( 'Classified Categories', 'taxonomy general name' ), | |
'singular_name' => _x( 'Classified Category', 'taxonomy singular name' ), | |
'search_items' => __( 'Search Classified Categories' ), | |
'all_items' => __( 'All Classified Categories' ), | |
'parent_item' => __( 'Parent Classified Category' ), | |
'parent_item_colon' => __( 'Parent Classified Category:' ), | |
'edit_item' => __( 'Edit Classified Category' ), | |
'update_item' => __( 'Update Classified Category' ), | |
'add_new_item' => __( 'Add New Classified Category' ), | |
'new_item_name' => __( 'New Classified Category' ), | |
'menu_name' => __( 'Classified Categories' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'rewrite' => array( 'slug' => 'classifieds', 'with_front' => false ), | |
); | |
register_taxonomy( 'classified_category', 'classifieds', $args ); | |
} | |
add_action( 'init', 'my_taxonomies_classified_category', 0 ); | |
/************************************************/ | |
add_filter('post_type_link', 'classifieds_permalink_structure', 10, 4); | |
function classifieds_permalink_structure($post_link, $post, $leavename, $sample) | |
{ | |
if ( false !== strpos( $post_link, '%classified_category%' ) ) | |
{ | |
$classified_type_term = get_the_terms( $post->ID, 'classified_category' ); | |
if(is_array($classified_type_term) && !empty($classified_type_term)) | |
{ | |
$post_link = str_replace( '%classified_category%', array_pop( $classified_type_term )->slug, $post_link ); | |
} | |
} | |
return $post_link; | |
} | |
/************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment