$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom Post Type Advanced Slug|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom Post Type Advanced Slug

matteradmin11PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far