admin管理员组文章数量:1130349
I defined a custom post type with rewrite rule:
register_post_type('balls', [
'labels' => [
'name' => 'balls',
'singular_name' => 'balls',
'add_new' => 'new',
'add_new_item' => 'new',
'parent_item_colon' => ''
],
'taxonomies' => ['category'],
'menu_position' => 4,
'public' => true,
'query_var' => true,
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail'],
'rewrite' => [
'slug' => 'ballinfo'
]
]);
now how do I know if a site was called with /ballinfo or not? How do I know if my rewrite rule was matched and used?
I defined a custom post type with rewrite rule:
register_post_type('balls', [
'labels' => [
'name' => 'balls',
'singular_name' => 'balls',
'add_new' => 'new',
'add_new_item' => 'new',
'parent_item_colon' => ''
],
'taxonomies' => ['category'],
'menu_position' => 4,
'public' => true,
'query_var' => true,
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail'],
'rewrite' => [
'slug' => 'ballinfo'
]
]);
now how do I know if a site was called with /ballinfo or not? How do I know if my rewrite rule was matched and used?
1 Answer
Reset to default 1I'm not very sure of what you're trying to achieve, but I hope this answer can help. :)
With the way your custom post type is being registered, WordPress will create custom rewrite rules such as below for the CPT's single post pages:
RegEx: ballinfo/([^/]+)(?:/([0-9]+))?/?$ Query: index.php?balls=$matches[1]&page=$matches[2]So for the following question:
how do I know if a site was called with
/ballinfoor notWhen the single page of a
ballspost is visited, the URL would have/ballinfo/as inexample/ballinfo/an-example-balls-post.Now to programmatically check if the URL contains
/ballinfo/, you can check if the$requestproperty of the globalWPclass instance starts with aballinfo/like so:global $wp; if ( preg_match( '#^ballinfo/#', $wp->request ) ) { echo 'Site was called with the /ballinfo<br>'; } echo '$wp->request is ' . $wp->request . '<br>';And for the following question:
How do I know if my rewrite rule was matched and used?
You can match the rule (RegEx) with the one in the
$matched_ruleproperty of the globalWPclass instance.For example, for the single
ballspost pages, where the rule uses the RegEx pattern as in point #1 in this answer, try this:global $wp; if ( 'ballinfo/([^/]+)(?:/([0-9]+))?/?$' === $wp->matched_rule ) { echo 'Yay, my rewrite rule was matched!<br>'; } else { echo 'Not matched. $wp->matched_rule is ' . $wp->matched_rule . '<br>'; }
And you may already know this, but if you just wanted to check if the requested URL is for a CPT post/archive/etc., you can use is_singular(), is_post_type_archive(), and other appropriate WordPress conditional functions/tags.
I defined a custom post type with rewrite rule:
register_post_type('balls', [
'labels' => [
'name' => 'balls',
'singular_name' => 'balls',
'add_new' => 'new',
'add_new_item' => 'new',
'parent_item_colon' => ''
],
'taxonomies' => ['category'],
'menu_position' => 4,
'public' => true,
'query_var' => true,
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail'],
'rewrite' => [
'slug' => 'ballinfo'
]
]);
now how do I know if a site was called with /ballinfo or not? How do I know if my rewrite rule was matched and used?
I defined a custom post type with rewrite rule:
register_post_type('balls', [
'labels' => [
'name' => 'balls',
'singular_name' => 'balls',
'add_new' => 'new',
'add_new_item' => 'new',
'parent_item_colon' => ''
],
'taxonomies' => ['category'],
'menu_position' => 4,
'public' => true,
'query_var' => true,
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail'],
'rewrite' => [
'slug' => 'ballinfo'
]
]);
now how do I know if a site was called with /ballinfo or not? How do I know if my rewrite rule was matched and used?
-
1
Go to
wp-admin/edit.php?post_type=ballsand mouseover the "View" link of any post, and see if the permalink has/ballinfo/{slug}. If yes, the rewrite rule is being applied. Then visit the permalink and if you see the proper post/content, the rewrite rule works properly. If not, visit the permalink settings page to flush the rewrite rules. – Sally CJ Commented Oct 27, 2018 at 15:09 -
1
If you meant by programmatically, you can get the rewrite rule that matches the requested URL via
WP::$matched_rule. – Sally CJ Commented Oct 27, 2018 at 15:33 -
1
Use either
global $wp; echo $wp->matched_rule;orecho $GLOBALS['wp']->matched_rule;– Sally CJ Commented Oct 27, 2018 at 16:14 -
1
There's probably an API function to accomplish what you are trying to do, like
is_singular( 'balls' );, but you have not described the problem you are trying to solve or given any details, like where and when you are trying to do this. – Milo Commented Oct 27, 2018 at 16:23 - 1 @JohnSmith, check my answer. – Sally CJ Commented Oct 28, 2018 at 7:13
1 Answer
Reset to default 1I'm not very sure of what you're trying to achieve, but I hope this answer can help. :)
With the way your custom post type is being registered, WordPress will create custom rewrite rules such as below for the CPT's single post pages:
RegEx: ballinfo/([^/]+)(?:/([0-9]+))?/?$ Query: index.php?balls=$matches[1]&page=$matches[2]So for the following question:
how do I know if a site was called with
/ballinfoor notWhen the single page of a
ballspost is visited, the URL would have/ballinfo/as inexample/ballinfo/an-example-balls-post.Now to programmatically check if the URL contains
/ballinfo/, you can check if the$requestproperty of the globalWPclass instance starts with aballinfo/like so:global $wp; if ( preg_match( '#^ballinfo/#', $wp->request ) ) { echo 'Site was called with the /ballinfo<br>'; } echo '$wp->request is ' . $wp->request . '<br>';And for the following question:
How do I know if my rewrite rule was matched and used?
You can match the rule (RegEx) with the one in the
$matched_ruleproperty of the globalWPclass instance.For example, for the single
ballspost pages, where the rule uses the RegEx pattern as in point #1 in this answer, try this:global $wp; if ( 'ballinfo/([^/]+)(?:/([0-9]+))?/?$' === $wp->matched_rule ) { echo 'Yay, my rewrite rule was matched!<br>'; } else { echo 'Not matched. $wp->matched_rule is ' . $wp->matched_rule . '<br>'; }
And you may already know this, but if you just wanted to check if the requested URL is for a CPT post/archive/etc., you can use is_singular(), is_post_type_archive(), and other appropriate WordPress conditional functions/tags.
本文标签: custom post typesHow do I know if a rewritten rule was applied
版权声明:本文标题:custom post types - How do I know if a rewritten rule was applied? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749227623a2335802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp-admin/edit.php?post_type=ballsand mouseover the "View" link of any post, and see if the permalink has/ballinfo/{slug}. If yes, the rewrite rule is being applied. Then visit the permalink and if you see the proper post/content, the rewrite rule works properly. If not, visit the permalink settings page to flush the rewrite rules. – Sally CJ Commented Oct 27, 2018 at 15:09WP::$matched_rule. – Sally CJ Commented Oct 27, 2018 at 15:33global $wp; echo $wp->matched_rule;orecho $GLOBALS['wp']->matched_rule;– Sally CJ Commented Oct 27, 2018 at 16:14is_singular( 'balls' );, but you have not described the problem you are trying to solve or given any details, like where and when you are trying to do this. – Milo Commented Oct 27, 2018 at 16:23