Custom Post Type Advanced Slug

admin2025-06-04  47

I've been searching for a little while now, and I'm not even sure if this is possible.

I have a custom post type 'holidays', for the post, with a category taxonomy 'locations' to put the posts into.

Slugs are:

domain/holidays/ # lists all holidays

domain/holidays/{post_title} # displays single post

domain/location/ # displays all location categories

domain/location/england # displays all posts in that category

Now I have a few custom fields within the locations taxonomy, which I would like to use in a type of SEO landing page, and not display them within the actual category archives page (which will show all the posts under that category)

So what I'm trying to gain is:

domain/location/england/overview - which will show all the SEO content about England - removing the /overview from the URL string will display all the location posts.

Is something like that even possible? I wanted to keep everything streamline and simple as possible on the backend, without having to create individual pages for each of the locations, which already exist as a category within the custom post type.

I've been searching for a little while now, and I'm not even sure if this is possible.

I have a custom post type 'holidays', for the post, with a category taxonomy 'locations' to put the posts into.

Slugs are:

domain/holidays/ # lists all holidays

domain/holidays/{post_title} # displays single post

domain/location/ # displays all location categories

domain/location/england # displays all posts in that category

Now I have a few custom fields within the locations taxonomy, which I would like to use in a type of SEO landing page, and not display them within the actual category archives page (which will show all the posts under that category)

So what I'm trying to gain is:

domain/location/england/overview - which will show all the SEO content about England - removing the /overview from the URL string will display all the location posts.

Is something like that even possible? I wanted to keep everything streamline and simple as possible on the backend, without having to create individual pages for each of the locations, which already exist as a category within the custom post type.

Share Improve this question asked Jan 8, 2019 at 21:40 James SimpsonJames Simpson 376 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This is pretty straightforward using a rewrite endpoint.

The first step is to set an ep_mask when you register your taxonomy, which is part of the rewrite argument. Note that it's a constant, and should be as-is, without quotes:

$args = array(
    'rewrite' => array(
        'slug' => 'location',
        'with_front' => false,
        'ep_mask' => EP_TAGS
    ),
    // your other args...
);

Now you can add a rewrite endpoint immediately after registering your taxonomy, also hooked to init:

add_rewrite_endpoint( 'overview', EP_TAGS );

Don't forget to flush rewrite rules after adding or changing anything in this code. You can do this quickly by visiting the Settings > Permalinks page.

Now you'll be able to visit

domain/location/england/

as well as

domain/location/england/overview/

To load a different template for these requests, we can use the taxonomy_template filter:

function wpd_location_overview_template( $template ){
    if( false !== get_query_var( 'overview', false ) ){
        $template = locate_template( 'location-overview.php' );
    }
    return $template;
}
add_filter( 'taxonomy_template', 'wpd_location_overview_template' );

This will load location-overview.php from your child or parent theme whenever overview is in the URL.

Use get_queried_object_id() or get_queried_object() in the template to fetch your fields associated to this term. The main query will also still contain the posts for this term, if you have any use for them.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749030029a315766.html

最新回复(0)