Dynamical slug on custom post type

I’m trying to custom urls of my custom post type.
I want to preprend a taxonomy term before the post name.

I create my post type with :

$args = array(
‘label’ => __( ‘Event’, ‘test’ ),
‘labels’ => $labels,
‘supports’ => array( ‘thumbnail’, ‘title’, ‘editor’ ),
‘taxonomies’ => array(),
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘menu_position’ => 20,
‘menu_icon’ => ‘dashicons-tickets-alt’,
‘show_in_admin_bar’ => true,
‘show_in_nav_menus’ => true,
‘can_export’ => true,
‘has_archive’ => true,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘capability_type’ => ‘post’,
‘rewrite’ => array(
‘slug’ => ‘%univers%’,
‘with_front’ => false,
),“
);

register_post_type( ‘event’, $args );

And i replace the %univers% with what i want :

function test_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == ‘event’ ){
$terms = wp_get_object_terms( $post->ID, ‘univers’ );
if( $terms ){
return str_replace( ‘%univers%’ , $terms[0]->slug , $post_link );
}else{
return str_replace( ‘%univers%’ , ” , $post_link );
}
}
return $post_link;
}
add_filter( ‘post_type_link’, ‘test_show_permalinks’, 12, 2 );

It’s works fine with my post type but broke all my other pages (404).

Anybody have any idea where that might have come from?

Thanks.

Read more here:: Dynamical slug on custom post type

Leave a Reply

Your email address will not be published. Required fields are marked *