Я определил два пользовательских типа сообщений:
У меня также есть 7 страниц с использованием определенного rubrique template: ( template-rubrique.php )
Обе мои Theme и Fiche имеют поле постобъекта ACF.
- Поле постобъекта Theme ACF используется для нацеливания на Rubrique .
- Поле постобъекта Fiche ACF используется для нацеливания на Тема .
Мне бы хотелось, чтобы мой URL-адрес CPTS был в следующем формате: example.com/myRubriqueName/myThemeName/myFicheName
.
myRubriqueName
- это страницаmyThemeName
и myFicheName
являются CPT.
До сих пор все мои URL-адреса fiche и theme хорошо сгенерированы, но в конечном итоге на странице 404.Кроме того, мои сообщения и страницы перенаправляются на первую страницу.
Я использую код из этого сообщения , который я пытался приспособить к моей ситуации.
Регистрация CPT:
register_post_type('theme', array(
'labels' => array(
'name' => 'Thèmes',
'singular_name' => 'Thème',
),
'public' => true,
'has_archive' => false,
'hierarchical' => false,
'menu_icon' => 'dashicons-art',
'rewrite' => array(
'slug' => '%rubrique%', // %rubrique% is used as placeholder
'with_front' => false
)
));
register_post_type('fiche', array(
'labels' => array(
'name' => 'Fiches',
'singular_name' => 'Fiche',
),
'public' => true,
'has_archive' => false,
'hierarchical' => false,
'menu_icon' => 'dashicons-clipboard',
'rewrite' => array(
'slug' => '%rubrique%/%theme%', // %rubrique%/%theme% is used as placeholder
'with_front' => false
),
));
Переписать правила
function fiche_rewrite() {
add_rewrite_tag(
'%theme%',
'([^&]+)',
'theme='
);
}
add_action( 'init', 'fiche_rewrite' );
function theme_rewrite() {
add_rewrite_tag(
'%rubrique%',
'([^&]+)',
'rubrique='
);
}
add_action( 'init', 'theme_rewrite' );
Перезапись заполнителя CPT
function gpc_custom_post_link_replacements( $post_link, $post ) {
$cpts = array('theme', 'fiche');
if ( empty( $post ) || !in_array($post->post_type, $cpts) ) {
return $post_link;
}
switch ($post->post_type) {
case 'fiche':
$theme_id = get_field('fiche-attachment', $post->ID);
$theme_slug = get_post_field( 'post_name', $theme_id );
$rubrique_id = get_field('theme-attachment', $theme_id);
$rubrique_slug = get_post_field('post_name', $rubrique_id);
if ( !empty( $theme_slug ) && !empty( $rubrique_slug ) ) {
$post_link = str_replace('%rubrique%', $rubrique_slug, $post_link );
$post_link = str_replace('%theme%', $theme_slug, $post_link );
}
break;
case 'theme':
$rubrique_id = get_field('theme-attachment', $post->ID);
$rubrique_slug = get_post_field('post_name', $rubrique_id);
if ( !empty( $rubrique_slug ) ) {
$post_link = str_replace('%rubrique%', $rubrique_slug, $post_link );
}
break;
}
return $post_link;
}
add_filter( 'post_type_link', 'wpc_custom_post_link_replacements', 9, 2 );
Несоответствие перенаправления связанных сообщений
function custom_post_redirects() {
global $post, $wp_query;
$redirect_to = get_home_url();
if( ! is_singular( 'fiche' ) && ! is_singular('theme') ) {
return;
}
if( is_singular('fiche') ) {
$given_slug = $wp_query->get( 'theme' );
$expected_theme = get_field('field-attachment', $post->ID );
if( empty( $given_slug ) || empty( $expected_theme ) ) {
wp_redirect( $redirect_to );
exit();
}
$expected_slug = get_post_field( 'post_name', $expected_theme );
if( $given_slug !== $expected_slug ) {
wp_redirect( $redirect_to );
exit();
}
}
else if( is_singular('theme' ) ) {
$given_slug = $wp_query->get( 'rubrique' );
$expected_rubrique = get_field('theme-attachment', $post->ID);
if( empty( $given_slug ) || empty( $expected_theme ) ) {
wp_redirect( $redirect_to );
exit();
}
$expected_slug = get_post_field( 'post_name', $expected_rubrique );
if( $given_slug !== $expected_slug ) {
wp_redirect( $redirect_to );
exit();
}
}
}
add_action( 'template_redirect', 'custom_post_redirects' );