-
-
Save fritids/8914612 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// Add filter to plugin init function | |
add_filter('post_type_link', function ($permalink, $post_id, $leavename) { | |
$post = get_post($post_id); | |
$rewritecode = array( | |
$leavename? '' : '%cursos%', | |
'%category%', | |
); | |
if ( '' != $permalink && $post->post_type=='cursos' && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) { | |
$category = ''; | |
if ( strpos($permalink, '%category%') !== false ) { | |
$cats = get_the_category($post->ID); | |
if ( $cats ) { | |
usort($cats, '_usort_terms_by_ID'); // order by ID | |
$category = $cats[0]->slug; | |
if ( $parent = $cats[0]->parent ) | |
$category = get_category_parents($parent, false, '/', true) . $category; | |
} | |
// show default category in permalinks, without | |
// having to assign it explicitly | |
if ( empty($category) ) { | |
$default_category = get_category( get_option( 'default_category' ) ); | |
$category = is_wp_error( $default_category ) ? '' : $default_category->slug; | |
} | |
} | |
$rewritereplace = array( | |
$post->post_name, | |
$category, | |
); | |
$permalink = str_replace($rewritecode, $rewritereplace, $permalink); | |
} else { // if they're not using the fancy permalink option | |
} | |
return $permalink; | |
}, 10, 3); | |
add_filter('post_rewrite_rules', function($rules){ | |
return array(); | |
}); | |
add_filter('date_rewrite_rules', function($rules){ | |
return array(); | |
}); | |
add_action('init', function (){ | |
global $wp_rewrite; | |
$cursos_structure = '/%category%/%cursos%'; | |
$wp_rewrite->add_rewrite_tag("%cursos%", '([^/]+)', "cursos="); | |
$wp_rewrite->add_permastruct('cursos', $cursos_structure, false); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment