最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add Rewrite Endpoint to CPT Archive

programmeradmin1浏览0评论

I have a Custom Post Type with a hierarchical taxonomy archive and want to add an endpoint page called "infopage" to each term in the archive.

e.g.

domain/tax-term-1/infopage

or

domain/tax-term-2/child-term-3/child-term-4/infopage

etc.

I don't want to add this as a child term, but rather want it to be a "virtual" page as the contents will be generated dynamically.

Tried something like this but was getting a 404 when accessing the endpoint page (yes, I flushed permalinks):

add_action( 'init', function() {
    add_rewrite_endpoint( 'infopage', EP_PAGES );
} );

add_action( 'template_redirect', function() {
    global $wp_query;
    if ( ! isset( $wp_query->query_vars['infopage'] ) ) {
        return;
    }
    include plugin_dir_path( __FILE__ ) . 'templates/infopage.php';
    die;
} );

Is add_rewrite_endpoint not meant to be used with CPTs and custom taxonomies? If not, is there a recommended alternative way to achieve what I need?

I have a Custom Post Type with a hierarchical taxonomy archive and want to add an endpoint page called "infopage" to each term in the archive.

e.g.

domain.com/tax-term-1/infopage

or

domain.com/tax-term-2/child-term-3/child-term-4/infopage

etc.

I don't want to add this as a child term, but rather want it to be a "virtual" page as the contents will be generated dynamically.

Tried something like this but was getting a 404 when accessing the endpoint page (yes, I flushed permalinks):

add_action( 'init', function() {
    add_rewrite_endpoint( 'infopage', EP_PAGES );
} );

add_action( 'template_redirect', function() {
    global $wp_query;
    if ( ! isset( $wp_query->query_vars['infopage'] ) ) {
        return;
    }
    include plugin_dir_path( __FILE__ ) . 'templates/infopage.php';
    die;
} );

Is add_rewrite_endpoint not meant to be used with CPTs and custom taxonomies? If not, is there a recommended alternative way to achieve what I need?

Share Improve this question edited Jan 27, 2022 at 22:55 Edegist asked Jan 27, 2022 at 20:11 EdegistEdegist 1331 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You're getting the 404 most likely because your custom taxonomy is not assigned to the endpoint mask (EP_PAGES) used with your endpoint.

Because add_rewrite_endpoint() does work with custom post types and taxonomies, but you need to assign the endpoint mask to the post type or taxonomy during registration, via the ep_mask key in the rewrite argument for register_taxonomy() as well as register_post_type(), like so:

// Add the "infopage" endpoint with the endpoint mask EP_PAGES.
add_rewrite_endpoint( 'infopage', EP_PAGES );

// Register a "foo" post type.
register_post_type( 'foo', array(
    'public'  => true,
    'label'   => 'Foos',
    'rewrite' => array(
        'ep_mask' => EP_PAGES, // assign EP_PAGES to the CPT
        'slug'    => 'foos',
    ),
) );

// Register a hierarchical taxonomy for the "foo" CPT above.
register_taxonomy( 'foo_cpt', 'foo', array(
    'public'       => true,
    'label'        => 'Foo Categories',
    'hierarchical' => true,
    'rewrite'      => array(
        'ep_mask'      => EP_PAGES, // assign EP_PAGES to the taxonomy
        'hierarchical' => true,     // this one is entirely up to you
    ),
) );

So assign the endpoint mask and then flush the rewrite rules (simply visit the Permalink Settings admin page), and see if you're still getting the 404.


Additionally, if you're unable to change the register_taxonomy() or register_post_type() code (e.g. because the post type or taxonomy is registered using a 3rd-party plugin), then you could use the register_taxonomy_args or register_post_type_args hook to modify the taxonomy or post type arguments.

And as for the "recommended alternative", then it would be using add_rewrite_rule() to manually add the rewrite rule for your endpoint, e.g. add_rewrite_rule( 'foo_cpt/(.+?)/infopage(/(.*))?/?$', 'index.php?foo_cpt=$1&infopage=$3', 'top' ) for the foo_cpt taxonomy above. :)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论