Created
October 19, 2013 12:38
-
-
Save leoken/7055243 to your computer and use it in GitHub Desktop.
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
//PUT THIS CODE INTO YOUR functions.php | |
//Creating custom taxonomy: | |
add_action( 'init', 'create__taxonomy' ); | |
function create__taxonomy() { | |
/* ########################################################## | |
* ### 'GALLERY' custom post type related custom taxonomy ### | |
* ########################################################## | |
*/ | |
/* | |
* Hi, we are 'Gallery Categories' - custom taxonomy of 'gallery' custom post type. | |
*/ | |
$labels = array( | |
'name' => _x( 'Gallery - Categories', 'taxonomy general name', 'translation-domain' ), | |
'singular_name' => _x( 'Gallery Category', 'taxonomy singular name', 'translation-domain' ), | |
'search_items' => __( 'Search Categories', 'translation-domain' ), | |
'all_items' => __( 'All Categories - Gallery', 'translation-domain' ), | |
'parent_item' => __( 'Parent Category', 'translation-domain' ), | |
'parent_item_colon' => __( 'Parent Category:', 'translation-domain' ), | |
'edit_item' => __( 'Edit Category - Gallery', 'translation-domain' ), | |
'menu_name' => __( 'Gallery Categories', 'translation-domain' ), | |
'update_item' => __( 'Update Category - Gallery', 'translation-domain' ), | |
'add_new_item' => __( 'Add New Gallery Category', 'translation-domain' ), | |
'new_item_name' => __( 'New Gallery Category Name', 'translation-domain' ), | |
); | |
register_taxonomy('gallery_category','gallery',array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
//'query_var' => true, | |
'query_var' => 'gallery_category', | |
'rewrite' => false | |
)); | |
// Register custom rewrite rules | |
// @source: http://wpquestions.com/question/show/id/2841 (last comments) | |
global $wp_rewrite; | |
$wp_rewrite->add_rewrite_tag('%gallery%', '([^/]+)', 'gallery='); | |
$wp_rewrite->add_rewrite_tag('%gallery_category%', '([^/]+)', 'gallery_category='); | |
$wp_rewrite->add_permastruct('gallery_category', '/galeria/kategoria-%gallery_category%', false, EP_CATEGORIES); | |
$wp_rewrite->add_permastruct('gallery', '/galeria/kategoria-%gallery_category%/%gallery%', false); | |
} | |
//Adding Rewrite rules: | |
// @source: http://wpquestions.com/question/show/id/2841 (last comments) | |
add_filter('post_type_link', 'wp_gallery_permalinks', 10, 3); | |
add_filter('term_link', 'wp_gallery_term_link', 10, 3); | |
function wp_gallery_permalinks($permalink, $post_id, $leavename) { | |
$no_terms = 'no-category'; | |
$post = get_post($post_id); | |
if($post->post_type != 'gallery' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) | |
return $permalink; | |
$terms = wp_get_object_terms((int)$post->ID, 'gallery_category'); | |
if(is_wp_error($terms) || empty($terms)) { | |
$term_slug = $no_terms; | |
} else { | |
$term = reset($terms); | |
if(is_object($term)) | |
$term_slug = $term->slug; | |
else | |
$term_slug = $no_terms; | |
} | |
$permalink = str_replace('%gallery_category%', $term_slug, $permalink); | |
return $permalink; | |
} | |
function wp_gallery_term_link($termlink, $term, $taxonomy) { | |
$termlink = str_replace('%gallery_category%', $term->slug, $termlink); | |
return $termlink; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment