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

Add a permalink variable onto custom post type URL after post name slug

programmeradmin7浏览0评论

I have a custom post type mycpt and I'm trying to allow for a variable to be appended onto the end of the URL right after the post name slug, like this:

www.site/mycpt/the-name-of-my-post/var-value-here/

I've been searching around, and the only examples I can find don't use the post name/slug in the URL, but rather taxonomies, so I'm not sure what the correct way to do it is. Here is what I'm trying now, but it's treating the URL with the variable as a separate page type (it's loading a default template rather than the template my custom post type uses).

add_action( 'init', function() {
    add_rewrite_tag( '%my_var%', '([^/]*)' );
    add_rewrite_rule( '^mycpt/(.*)/([^/]*)/?', 'index.php?post_type=mycpt&my_var=$matches[1]', 'top' );
}, 10, 0 );

I also tried changing $matches[1] to $matches[2] since I thought maybe the wildcard for the post name/slug was the first match, but that didn't work either.

Can anybody see what I'm doing wrong here?

I have a custom post type mycpt and I'm trying to allow for a variable to be appended onto the end of the URL right after the post name slug, like this:

www.site/mycpt/the-name-of-my-post/var-value-here/

I've been searching around, and the only examples I can find don't use the post name/slug in the URL, but rather taxonomies, so I'm not sure what the correct way to do it is. Here is what I'm trying now, but it's treating the URL with the variable as a separate page type (it's loading a default template rather than the template my custom post type uses).

add_action( 'init', function() {
    add_rewrite_tag( '%my_var%', '([^/]*)' );
    add_rewrite_rule( '^mycpt/(.*)/([^/]*)/?', 'index.php?post_type=mycpt&my_var=$matches[1]', 'top' );
}, 10, 0 );

I also tried changing $matches[1] to $matches[2] since I thought maybe the wildcard for the post name/slug was the first match, but that didn't work either.

Can anybody see what I'm doing wrong here?

Share Improve this question asked Jul 4, 2018 at 18:19 GavinGavin 4247 silver badges21 bronze badges 4
  • WordPress doesn't know which post you want, you only pass the value of my_var in your rewrite rule. – Milo Commented Jul 4, 2018 at 19:50
  • @Milo do I need to manually add the &p=xxx url parameter? I figured WordPress automatically added that, since none of the examples I found added that parameter. – Gavin Commented Jul 4, 2018 at 19:51
  • Yes, but not p, it's whatever your post type slug is, mycpt in your example. Rewrite rules need to set all the necessary query vars to result in a successful main query. – Milo Commented Jul 4, 2018 at 19:54
  • I actually wasn't able to get this working. I ended up just using add_rewrite_endpoint and having a variable name in the permalink structure. – Gavin Commented Jul 6, 2018 at 18:38
Add a comment  | 

2 Answers 2

Reset to default 5

Here's a complete working example that adds a post type, with extra rule to capture an additional parameter:

function wpd_post_type_and_rule() {
    register_post_type( 'mycpt',
        array(
            'labels' => array(
                'name' => __( 'mycpt' ),
            ),
            'public' => true,
            'rewrite' => array( 'slug' => 'mycpt' ),
        )
    );
    add_rewrite_tag( '%mycpt_var%', '([^/]*)' );
    add_rewrite_rule(
        '^mycpt/([^/]*)/([^/]*)/?$',
        'index.php?mycpt=$matches[1]&mycpt_var=$matches[2]',
        'top'
    );
}
add_action( 'init', 'wpd_post_type_and_rule' );

After adding this and flushing rewrite rules, you'll have both

www.site/mycpt/the-name-of-my-post/

and

www.site/mycpt/the-name-of-my-post/var-value-here/

You can get the value of mycpt_var in the template with:

echo get_query_var( 'mycpt_var' );

As a temporary solution you can try using free plugin : https://wordpress/plugins/custom-post-type-permalinks/

发布评论

评论列表(0)

  1. 暂无评论