Last active
February 27, 2020 12:43
-
-
Save mbootsman/ba43f651a995f353fca3ff8d7bc10483 to your computer and use it in GitHub Desktop.
Custom post type permalink structure with hyphen instead of slash
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 for creating a custom post link | |
add_filter( 'post_type_link', 'nstrm_changepost_link_slug', 10, 3 ); | |
function nstrm_changepost_link_slug( $post_link, $post, $leavename ) { | |
if ( 'post-type-name' != $post->post_type || 'publish' != $post->post_status ) { | |
return $post_link; | |
} | |
$post_link = str_replace( '/text-/', '/text-', $post_link ); | |
return $post_link; | |
} | |
// Custom rewrite to use the custom post link | |
add_action( 'generate_rewrite_rules', 'nstrm_add_rewrite_rules' ); | |
function nstrm_add_rewrite_rules($wp_rewrite) { | |
$cpt_rule = array( | |
'text-(.+)?' => 'index.php?post_type=post-type-name&post-type-name=' . $wp_rewrite->preg_index( 1 ), | |
); | |
$wp_rewrite->rules = $cpt_rule + $wp_rewrite->rules; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah good addition, thanks!