Après avoir regardé la documentation et fait des recherches, je n'ai rien trouvé, alors voici:
Existe-t-il un moyen de personnaliser l'héritage post-type et une structure d'URL comme celle-ci
domain.com/%parenttype%/%parentname%/%posttype/%postname%/
Disons que les artistes ont des chansons, ressemblerait à ceci:
domain.com/artist/john-lennon/songs/imagine/
Je n'ai jamais vu cela en développement auparavant. Est-ce que quelqu'un sait quelque chose à ce sujet?
Et c'est parti:
parent-type/%parent-slug%/child-slug
( Attention : n'utilisez pas le même mot pour parent-type
et parent-slug
; parent-slug
devrait correspondre à votre slug parent )J'ai tout emballé, prêt à être utilisé:
class myArtistSongs{
function init_post_types() {
$args = array(
'menu_position' => 8,
'labels' => array(
'name' => __('Songs', 'myArtistSongs'),
'all_items' => __('All Songs', 'myArtistSongs'),
'singular_name' => __('Song', 'myArtistSongs')
),
'menu_icon' => 'dashicons-format-audio',
'public' => true,
'publicly_queryable' => true,
'query_var' => false,
'capability_type' => 'post',
'has_archive' => false,
'rewrite' => array(
'slug' => 'artist/%artist%/songs',
'feeds' => true,
'pages' => true,
'with_front' => true,
),
'taxonomies' => array(
'artist'
),
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt'
)
);
register_post_type( 'song', $args);
$args = array(
'menu_position' => 8,
'labels' => array(
'name' => __('Artists', 'myArtistSongs'),
'all_items' => __('All Artists', 'myArtistSongs'),
'singular_name' => __('Artist', 'myArtistSongs')
),
'menu_icon' => 'dashicons-groups',
'public' => true,
'publicly_queryable' => true,
'query_var' => false,
'capability_type' => 'post',
'has_archive' => false,
'rewrite' => array(
'slug' => 'artists',
'feeds' => true,
'pages' => true,
'with_front' => true,
),
'taxonomies' => array(
'artist'
),
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt'
)
);
register_post_type( 'artist', $args);
}
static function song_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%artist%') === FALSE) return $permalink;
$post = get_post($post_id);
if (!$post) return $permalink;
$artist = get_post_meta($post->ID, '_artist', true);
if ($artist > 0) {
$artist = get_post($artist);
if (!$artist) $artist = false;
else $artist = $artist->post_name;
}
if (!$artist) $artist = 'no-artist';
return str_replace('%artist%', $artist, $permalink);
}
function add_meta_box() {
add_meta_box(
'article-parent',
__('Artist',''),
array('myArtistSongs', 'meta_box'),
'song',
'side',
'low'
);
}
function meta_box($post) {
$artist = get_post_meta($post->ID, '_artist', true);
global $wp_query, $post;
$old_wp_query = $wp_query;
$old_post = $post;
$loop = new WP_Query(array('post_type' => 'artist', 'posts_per_page=-1'));
echo "<select name='artist' id='artist'>";
$no_value = __('(no artist)','myArtistSongs');
echo "<option value=''".(empty($artist)?" selected='selected'":'').">".$no_value."</option>";
if($loop->have_posts()) while ($loop->have_posts()) {
$loop->the_post();
$o_id = get_the_ID();
$o = get_the_title();
echo "<option value='{$o_id}'".(!empty($artist) && $artist == $o_id?" selected='selected'":'').">{$o}</option>";
}
echo "</select>";
$post = $old_post;
$wp_query = $old_wp_query;
}
function save($post_id) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if ($_POST['artist']) {
update_post_meta($post_id, '_artist', $_POST['artist']);
} else {
delete_post_meta($post_id, '_artist');
}
}
}
add_action('init', array('myArtistSongs', 'init_post_types'));
add_filter('post_link', array('myArtistSongs', 'song_permalink'), 10, 3);
add_filter('post_type_link', array('myArtistSongs', 'song_permalink'), 10, 3);
add_action('add_meta_boxes', array('myArtistSongs', 'add_meta_box'));
add_action( 'save_post', array('myArtistSongs','save') );