- .com/CPT/ (Архив пользовательского типа Post)
- .com/CPT/post-name (отдельный пост в пользовательском типе сообщения)
- .com/CPT/category-name (архив категории CPT)
- .com/CPT/category-name/post-name (отдельный пост в пользовательской категории типа пост)
Я прочитал и попытался использовать здесь другое решение - но опять же, они проигнорировали необходимость в структуре of .com/cpt/post-name .
По сути, мне нужно убедиться, что слизняк для пользовательского типа поста и слизняка для пользовательской категории типа Post Type То же самое - вот несколько конкретных примеров значимых URL -адресов: < /p>
[*].com/services (Архив пользовательского типа Post)
[*].com/services/botox (отдельный пост пользовательского типа сообщения)
[*].com/services/injectables (Архив пользовательского типа Post Type Custom Таксономия)
.com/services/injectables/botox (отдельная публикация пользовательской публикации типа «Пользовательская таксономия») 2 Структура: < /p>
// Register the Custom Post Type
function register_custom_post_type() {
$args = array(
'labels' => array(
'name' => 'Services',
'singular_name' => 'Service',
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'services', // Base slug for your CPT
'with_front' => false,
'hierarchical' => true, // Allows for /services/category/post
),
'show_in_rest' => true, // Enable Gutenberg editor if needed
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
'taxonomies' => array('services_category'), // Attach custom taxonomy if needed
);
register_post_type('services', $args);
}
add_action('init', 'register_custom_post_type');
// Register Custom Taxonomy for the Custom Post Type
function register_custom_taxonomy() {
$args = array(
'labels' => array(
'name' => 'Services Categories',
'singular_name' => 'Services Category',
),
'public' => true,
'rewrite' => array(
'slug' => 'services', // This will make the taxonomy appear under /services/category-name/
'with_front' => false,
'hierarchical' => true, // Ensure categories are hierarchical (like regular categories)
),
'hierarchical' => true,
);
register_taxonomy('services_category', 'services', $args);
}
add_action('init', 'register_custom_taxonomy');
**// Custom Rewrite Rules to Match the Permalink Structure
function custom_rewrite_rules($rules) {
$new_rules = array(
// CPT Archive: /services/
'services/?$' => 'index.php?post_type=services',
// Single CPT Post: /services/post-name/
'services/([^/]+)/?$' => 'index.php?post_type=services&name=$matches[1]',
// Custom Taxonomy Archive: /services/category-name/
// This rule should be placed *after* the CPT single post rule to avoid conflicts
'services/([^/]+)/?$' => 'index.php?taxonomy=services_category&term=$matches[1]',
// Single CPT Post within a Category: /services/category-name/post-name/
'services/([^/]+)/([^/]+)/?$' => 'index.php?post_type=services&services_category=$matches[1]&name=$matches[2]',
);
return $new_rules + $rules;
}
add_filter('rewrite_rules_array', 'custom_rewrite_rules');
// Add rewrite tag for custom taxonomy (to ensure taxonomy permalinks work)
function add_custom_rewrite_tags() {
add_rewrite_tag('%services_category%', '([^&]+)');
}
add_action('init', 'add_custom_rewrite_tags');
Подробнее здесь: https://stackoverflow.com/questions/794 ... -wordpress
Мобильная версия