我们开始吧:
注册您的两个CPT(=自定义帖子类型),例如艺术家、歌曲,将子CPT中的重写段标编辑为parent-type/%parent-slug%/child-slug
(Attention: 不要使用相同的单词parent-type
和parent-slug
; parent-slug
应与您的家长CPT slug匹配)为您的孩子CPT添加一个元框,您可以在其中定义家长CPT,根据您的需要修改孩子CPT permalink
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\') );