Created
November 23, 2016 14:31
-
-
Save lukewhitehouse/c937d57309425b64cfe429626653a24f to your computer and use it in GitHub Desktop.
WordPress post type permalinks
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 | |
/** | |
* Modify Work permalink to add in category | |
*/ | |
function mixd_work_permalink_setup( $post_link, $id = 0, $leavename = FALSE ) { | |
// Test params are anywhere within the URL | |
if ( strpos('%work_type%', $post_link) === 0 ) { | |
return $post_link; | |
} else { | |
// Get the posts data | |
$post = get_post($id); | |
// If the $post data is an object and has a post_type of 'case-studies' | |
if ( !is_object($post) || $post->post_type != 'work' ) { | |
return $post_link; | |
} else { | |
// Get the $post's terms | |
$terms = wp_get_object_terms($post->ID, 'work_type'); | |
// If there are no terms | |
if ( !$terms ) { | |
// change params to | |
return str_replace('%work_type%', '', $post_link); | |
} else { | |
// change params to term slug | |
return str_replace('%work_type%', $terms[0]->slug, $post_link); | |
} | |
} | |
} | |
} | |
add_filter('post_type_link', 'mixd_work_permalink_setup', 1, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment