I think this question was answered a lot, but despite all threads I couldn't find a valid answer:
I'm working on a website with Wordpress (and without any plugin).
Here the creation of my custom post type 'actuality' and the taxonomy associated named 'actuality_type:
<?php
function create_actuality_taxonomy() {
$labels = array(
'name' => _x( 'Type d\'actualité', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Type d\'actualité', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Chercher un type', 'textdomain' ),
'all_items' => __( 'Tout les types', 'textdomain' ),
'edit_item' => __( 'Editer le type', 'textdomain' ),
'update_item' => __( 'Mettre à jour le type', 'textdomain' ),
'add_new_item' => __( 'Ajouter un nouveau type', 'textdomain' ),
'new_item_name' => __( 'Nom du nouveau type', 'textdomain' ),
'menu_name' => __( 'Type d\'actualité', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array(
'slug' => 'actuality', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
),
'show_in_rest' => true,
'show_in_quick_edit'=> false,
'public' => true,
'meta_box_cb' => false,
'show_in_nav_menus' => false,
'has_archive' => true
);
//first name of the taxonomy, second post_type name
register_taxonomy( 'actuality_type', 'actuality', $args );
}
function create_actuality_custom_post_type() {
$labels = array(
'name' => _x( 'Actualités', 'Post Type General Name'),
'singular_name' => _x( 'Actualité', 'Post Type Singular Name'),
'menu_name' => __( 'Ajouter une Actu'),
'all_items' => __( 'Toutes les Actualités'),
'view_item' => __( 'Voir les actualités'),
'add_new_item' => __( 'Ajouter une nouvelle Actualité'),
'add_new' => __( 'Ajouter'),
'edit_item' => __( 'Editer l\'Actualité'),
'update_item' => __( 'Modifier l\'Actualité'),
'search_items' => __( 'Rechercher une Actualité'),
'not_found' => __( 'Non trouvée'),
'not_found_in_trash' => __( 'Non trouvée dans la corbeille'),
);
$args = array(
'label' => __('actuality'),
'description' => __( 'Toute l\'actualité'),
'menu_icon' => 'dashicons-admin-site',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields' ),
'show_in_rest' => true,
'hierarchical' => false,
'public' => true,
'publicly_queryable' => true,
'menu_position' => 4,
'has_archive' => 'actuality',
'rewrite' => array(
'slug' => 'actuality/%actuality_type%', 'with_front' => false
),
'capability_type' => 'post',
);
register_post_type( 'actuality', $args );
}
Now, on my function.php file, this is how I implement those:
/*
* Add the menu "Create News" and Custom post typr on the admin page
*/
add_action('init', 'create_actuality_custom_post_type', 0);
/*
* Register new taxonomy for Actualities: News, Event and Salon
*/
add_action('init', 'create_actuality_taxonomy', 0);
/*
* Change permalink for actuality taxonomy
*/
function filter_post_type_link_for_actuality($link, $post) {
if ( $post->post_type !== 'actuality' ) {
return $link;
}
if ( $cats = get_the_terms($post->ID, 'actuality_type') ) {
$link = str_replace('%actuality_type%', array_pop($cats)->slug, $link);
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link_for_actuality', 10, 2);
The menu appears on my administration page, I can create some 'actuality_type' like 'salons'.
Now, if I make a new 'actuality' with 'Salons for ever' as a title and I set the 'actuality_type' on salons, the permalink is: http://localhost/mywordpress/actuality/salons/salons-for-ever/
But i have a 404 on this link and I don't know why. I acted empirically by creating a lot of file like
actuality-salons.php actuality_type-salons.php archive-actuality-salons.php archive-actuality_type-salons.php page-salons.php page-actuality-salons.php page-actuality_type-salons.php single-actuality-salons.php single-actuality_type-salons.php taxonomy-actuality-salons.php taxonomy-actuality_type-salons.php But none works.
I already try to use flush_rewrite_rules( false ); or reload the permalinks on the settings.
Thanks a lot everyone
Have a nice day