generated from pascalmartineau/wp-skeleton
feat: project permalink using ID
This commit is contained in:
@@ -45,7 +45,7 @@ function ccat_project_register_post_type() {
|
||||
'supports' => array( 'title', 'thumbnail', 'excerpt', 'revisions' ),
|
||||
'has_archive' => false,
|
||||
'rewrite' => array(
|
||||
'slug' => 'projet',
|
||||
'slug' => 'projet/%project_id%',
|
||||
'with_front' => false,
|
||||
),
|
||||
'query_var' => true,
|
||||
@@ -86,3 +86,18 @@ function ccat_project_post_updated_messages( $messages ) {
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
// Add custom rewrite rules for project ID permalinks
|
||||
add_action( 'init', 'ccat_project_add_rewrite_rules' );
|
||||
function ccat_project_add_rewrite_rules() {
|
||||
add_rewrite_rule( '^projet/([0-9]+)/?$', 'index.php?post_type=project&p=$matches[1]', 'top' );
|
||||
}
|
||||
|
||||
// Filter project permalinks to use ID instead of slug
|
||||
add_filter( 'post_type_link', 'ccat_project_custom_permalink', 10, 2 );
|
||||
function ccat_project_custom_permalink( $permalink, $post ) {
|
||||
if ( get_post_type( $post ) === 'project' ) {
|
||||
$permalink = home_url( "/projet/$post->ID/" );
|
||||
}
|
||||
return $permalink;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user