Detect page type by url (Archive, single, page, author,…)

You 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;

Leave a Comment