Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active November 2, 2017 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/07912f87e9761bd66e0bd8a757a12c1c to your computer and use it in GitHub Desktop.
Save igorbenic/07912f87e9761bd66e0bd8a757a12c1c to your computer and use it in GitHub Desktop.
custom-tax-question
<?php
//Add Custom WP Rewrite Rule to add taxo to link for Courses CPT
function resources_cpt_generating_rule( $wp_rewrite ) {
$rules = array();
$terms = get_terms( array(
‘taxonomy’ => ‘cours_topic’,
‘hide_empty’ => false,
));
$post_type = ‘course_cpt’;
foreach ($terms as $term) {
$rules[‘cours/’ . $term->slug . ‘/([^/]*)$’] = ‘index.php?post_type=’ . $post_type. ‘&course_cpt=$matches[1]&name=$matches[1]’;
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(‘generate_rewrite_rules’, ‘resources_cpt_generating_rule’);
function change_link( $permalink, $post ) {
if( $post->post_type == ‘course_cpt’ ) {
$resource_terms = get_the_terms( $post, ‘cours_topic’ );
$term_slug = ”;
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
// The featured resource will have another category which is the main one
if( $term->slug == ‘featured’ ) {
continue;
}
$term_slug = $term->slug;
break;
}
}
$permalink = get_home_url() .”/cours/” . $term_slug . ‘/’ . $post->post_name;
}
return $permalink;
}
add_filter(‘post_type_link’,”change_link”,10,2);
//Add Custom WP Rewrite Rule to add taxo to link for Podcast CPT
function podcast_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
‘taxonomy’ => ‘podcast_topic’,
‘hide_empty’ => false,
) );
$post_type = ‘podcast_cpt’;
foreach ($terms as $term) {
$rules[‘podcast/’ . $term->slug . ‘/([^/]*)$’] = ‘index.php?post_type=’ . $post_type. ‘&podcast_cpt=$matches[1]&name=$matches[1]’;
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(‘generate_rewrite_rules_podcast’, ‘podcast_cpt_generating_rule’);
function podcast_change_link( $permalink, $post ) {
if( $post->post_type == ‘podcast_cpt’ ) {
$resource_terms = get_the_terms( $post, ‘podcast_topic’ );
$term_slug = ”;
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
// The featured resource will have another category which is the main one
if( $term->slug == ‘featured’ ) {
continue;
}
$term_slug = $term->slug;
break;
}
}
$permalink = get_home_url() .”/podcast/” . $term_slug . ‘/’ . $post->post_name;
}
return $permalink;
}
add_filter(‘post_type_link’,”podcast_change_link”,10,2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment