Consider I've 3 template files

common.php <-- required_once by both archive.php & archive-books.php 

So whether archive-books.php or archive.php is being loaded depends on the current post type.

Since they both include common.php, in the common.php, how to I know which current WordPress template is picked? i.e. archive.php or archive-books.php ?

  • 2
    And when/where do you want to obtain that information, because this is important...? – Krzysiek Dróżdż Apr 2 at 9:40

In this specific case you could simply follow the logic: determine if you're on an archive page and what the current post type is. Like this:

if (is_archive() && 'books' == get_post_type) { do your thing }

Or you could use the function specific for checking this:

if (is_post_type_archive('books')) { do your thing }
  • That tells them what archive or post type they're on, not which file is being used. – Jacob Peattie Apr 2 at 10:32
  • I'm aware of that. But the question is, if you're inside common.php, how do you know by which template it was called? You can know that by looking at the post&archive type, because that determines which template it was. Assuming, indeed, that the normal WP hierarchy is followed. – cjbj Apr 2 at 10:39

try this to check if your specific post type archive file is present

$postType = get_query_var( 'post_type' );
    // You have the specific archive file for the post type
    // You don't have the specific archive file for the post type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.