0

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.com/holidays/ # lists all holidays

domain.com/holidays/{post_title} # displays single post

domain.com/location/ # displays all location categories

domain.com/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.com/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.

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.com/location/england/

as well as

domain.com/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.

  • I'm going to give this a try now, but I totally didn't think of doing it that way! You sir are a star! – James Simpson Jan 9 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.