admin管理员组文章数量:1317909
Update:Really short version
How do I get $wp_query
for a given url which can be anything (single, page archive, custom post type, author, year, month,...)?
TL;DR
I need a function which can return this info (as a REST API endpoint) based on a given url:
request:'category/Uncategorized'
response:
{
type:"category",
category_name:"uncategorized"
}
request:'books/harry-potter'
response:
{
type:"single",
post_type:"books",
name:"harry-potter"
}
So my app will know which template to use and what sort of data to expect.
The long story
I'm writing a Wordpress theme with ReactJS, I have my (known) routes set up with react-router, But I need to detect what sort of content I should expect if the requested url is not known at the present.
In case url is GET ed by browser(not as ajax call), I have my wordpress template file inject all the required data in the page (with $wp_query->query_vars
), So with a simple url check I'll know if data is meant for current url and if so, I'll use it.
The problem comes in when user navigates through and next pages load by calling respective API endpoints.
It becomes worse if I want to support all permalink structures, So I think I'll definitely need to resolve URLs in the back-end to check all the possibilities.
So I have a detector js which tries to see if it can detect what kind of resource is requested (by checking if url params contain category
,author
,...) and make the API call, in case it couldn't, it then will call the detector endpoint in which I need to tell my app what should be rendered in this page.
For example, I've currently registered a custom post type named "Team", so I already know how to handle mywebsite/team
& mywebsite/team/:slug
, but in case user registers a new post type, say "Books", my app will have no idea whether books is a page's slug, post's slug or a post type archive name.
Update:Really short version
How do I get $wp_query
for a given url which can be anything (single, page archive, custom post type, author, year, month,...)?
TL;DR
I need a function which can return this info (as a REST API endpoint) based on a given url:
request:'category/Uncategorized'
response:
{
type:"category",
category_name:"uncategorized"
}
request:'books/harry-potter'
response:
{
type:"single",
post_type:"books",
name:"harry-potter"
}
So my app will know which template to use and what sort of data to expect.
The long story
I'm writing a Wordpress theme with ReactJS, I have my (known) routes set up with react-router, But I need to detect what sort of content I should expect if the requested url is not known at the present.
In case url is GET ed by browser(not as ajax call), I have my wordpress template file inject all the required data in the page (with $wp_query->query_vars
), So with a simple url check I'll know if data is meant for current url and if so, I'll use it.
The problem comes in when user navigates through and next pages load by calling respective API endpoints.
It becomes worse if I want to support all permalink structures, So I think I'll definitely need to resolve URLs in the back-end to check all the possibilities.
So I have a detector js which tries to see if it can detect what kind of resource is requested (by checking if url params contain category
,author
,...) and make the API call, in case it couldn't, it then will call the detector endpoint in which I need to tell my app what should be rendered in this page.
For example, I've currently registered a custom post type named "Team", so I already know how to handle mywebsite/team
& mywebsite/team/:slug
, but in case user registers a new post type, say "Books", my app will have no idea whether books is a page's slug, post's slug or a post type archive name.
1 Answer
Reset to default 1You have rewrite_rules
in wp_options table. From there your app can have idea of what the wordpress is going to rewrite in future and how it is going to rewrite after index.php. You can use regex in JS with wp_options data to customise your app response.
UPDATE
Here is block of code I needed to determine the nature of current page/post to dynamically use different template based on the return-
if(!function_exists('get_nature_of_post')):
function get_nature_of_post() {
global $wp_query;
$wpq = json_decode(json_encode($wp_query),true);
$check = array("is_single","is_page","is_archive","is_author","is_category","is_tax","is_search","is_home","is_404","is_post_type_archive");
$page_identifiers = array_intersect_key($wpq,array_flip($check));
$page_identifiers = array_filter($page_identifiers);
$keys = array_flip(array_keys($page_identifiers));
$case['home'] = array_flip(array('is_home'));
$case['search'] = array_flip(array('is_search'));
$case['archive'] = array_flip(array('is_archive','is_post_type_archive'));
$case['taxonomy'] = array_flip(array('is_archive','is_tax'));
$case['single'] = array_flip(array('is_single'));
$case['page'] = array_flip(array('is_page'));
$home = !array_diff_key($case['home'], $keys) && !array_diff_key($keys, $case['home']);
$archive = !array_diff_key($case['archive'], $keys) && !array_diff_key($keys, $case['archive']);
$search = !array_diff_key($case['search'], $keys) && !array_diff_key($keys, $case['search']);
// var_dump($archive);
$taxonomy = !array_diff_key($case['taxonomy'], $keys) && !array_diff_key($keys, $case['taxonomy']);
// var_dump($taxonomy);
$single = !array_diff_key($case['single'], $keys) && !array_diff_key($keys, $case['single']);
// var_dump($single);
$page = !array_diff_key($case['page'], $keys) && !array_diff_key($keys, $case['page']);
// var_dump($page);
switch (!false) {
case $archive: return 'archive'; break;
case $taxonomy: return 'taxonomy'; break;
case $single: return 'single'; break;
case $page: return 'page'; break;
case $search: return 'search'; break;
case $home: return 'home'; break;
default: return false;
}
}
endif;
本文标签:
版权声明:本文标题:url rewriting - Detect page type by url (Archive, single, page, author,...) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741199795a2356989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论