I have registered a custom taxonomy and post type to handle the display of photo galleries on my website. I am needing to setup the permalinks to look like this: domain.com/photos/region/country/location or domain.com/photos/country/location depending on the selected terms for each post type. The region and country terms would be dependent on if the parent terms are Canada, United States, or World.
An example:
- Canada (parent term)
- Alberta (child term)
- United States (parent term)
- Arizona (child term)
- World (parent term)
- France (child term)
Register taxonomy code
register_taxonomy( 'photo_galleries', array( 'photos' ),
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'galleries',
'with_front' => false
),
));
Custom post type arguments
$args = array(
'supports' => $supports,
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'rewrite' => array(
'slug' => 'photos',
'with_front' => false
),
'menu_position' => 5,
'menu_icon' => 'dashicons-format-gallery',
'has_archive' => true,
);
register_post_type( 'photos', $args );
Galleries post_type_link function
function galleries_post_type_link( $url, $post ) {
if ( $post->post_type == 'photos' ) {
global $post;
$terms = get_the_terms( $post->id, 'photo_galleries' );
$term = $terms[0]->slug;
$url = str_replace('photos/', 'photos/' . $term . '/', $url);
}
return $url;
}
add_filter('post_type_link', 'galleries_post_type_link', 10, 4);
Right now this code is showing the following permalink structure in the admin panel custom post editor: domain.com/photos/france/bordeaux/. This post has a parent term of World and a child term of France. The term France has been set to primary, but making World the primary term has no effect on the permalink. As you can see, France is only being used in the permalink.
I have tried the following custom permalink structures with no success: /%category%/%postname%/
and /%postname%/
.
Any help is greatly appreciated.
Cheers,