Created
May 14, 2015 06:44
-
-
Save duogeekdev/d146d97e3c19008eb9b1 to your computer and use it in GitHub Desktop.
Append post ID at the end of the URL for a custom post type
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 | |
function change_post_type_link( $link, $post = 0 ){ | |
if ( $post->post_type == 'product' ){ | |
return home_url( 'product/'. $post->post_name .'/'. $post->ID ); | |
} else { | |
return $link; | |
} | |
} | |
add_filter('post_type_link', 'change_post_type_link', 10, 2); | |
add_action( 'init', 'change_rewrites_init' ); | |
function change_rewrites_init(){ | |
add_rewrite_rule( | |
'product/([a-z-]+)/([0-9]+)?$', | |
'index.php?post_type=product&p=$matches[1]&postid=$matches[2]', | |
'top' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment