Sitemap
developer_word

developer_word

custom_permalink

1 min readJan 19, 2026
//functions.php

function register_cluster_taxonomy() {

$labels = array(
'name' => _x( 'Clusters', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Cluster', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Clusters', 'textdomain' ),
'popular_items' => __( 'Popular Clusters', 'textdomain' ),
'all_items' => __( 'All Clusters', 'textdomain' ),
'edit_item' => __( 'Edit Cluster', 'textdomain' ),
'update_item' => __( 'Update Cluster', 'textdomain' ),
'add_new_item' => __( 'Add New Cluster', 'textdomain' ),
'new_item_name' => __( 'New Cluster Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate clusters with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove clusters', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used clusters', 'textdomain' ),
'menu_name' => __( 'Clusters', 'textdomain' ),
);

$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'cluster' ),
'show_in_rest' => true,
);

register_taxonomy( 'cluster', 'post', $args );
}
add_action( 'init', 'register_cluster_taxonomy' );

function multi_level_category_permalink($permalink, $post) {
if ($post->post_type == 'post') {

$categories = get_the_terms($post->ID, 'category');

$cluster = get_the_terms($post->ID, 'cluster');


if ($categories && !is_wp_error($categories)) {

$child = null;
foreach ($categories as $cat) {
if ($cat->parent != 0) {
$child = $cat;
}
}

if ($child) {
$parent = get_term($child->parent, 'category');
$cat_path = $parent->slug . '/' . $child->slug;
} else {
$cat_path = $categories[0]->slug;
}

if($cluster){
$cat_path = $cat_path . '/' . $cluster[0]->slug;
}

$permalink = str_replace('%categoryabc%', $cat_path, $permalink);

}
}
return $permalink;
}
add_filter('post_link', 'multi_level_category_permalink', 10, 2);

function add_category_hierarchy_rewrite_rules() {
add_rewrite_tag('%categoryabc%', '([^/]+(?:/[^/]+)*)');
add_permastruct('post', '%categoryabc%/%postname%/', false);
}
add_action('init', 'add_category_hierarchy_rewrite_rules');

/%categoryabc%/%postname%/

--

--