admin管理员组

文章数量:1025481

Consider I've 3 template files

archive.php 
archive-books.php 
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 ?

Consider I've 3 template files

archive.php 
archive-books.php 
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 ?

Share Improve this question edited Apr 2, 2019 at 10:05 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 2, 2019 at 9:36 YogaYoga 9192 gold badges20 silver badges39 bronze badges 1
  • 2 And when/where do you want to obtain that information, because this is important...? – Krzysiek Dróżdż Commented Apr 2, 2019 at 9:40
Add a comment  | 

2 Answers 2

Reset to default 0

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 }

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

$postType = get_query_var( 'post_type' );
if(file_exists('archive-'.$postType.'.php')){
    // You have the specific archive file for the post type
}else{
    // You don't have the specific archive file for the post type
}

Consider I've 3 template files

archive.php 
archive-books.php 
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 ?

Consider I've 3 template files

archive.php 
archive-books.php 
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 ?

Share Improve this question edited Apr 2, 2019 at 10:05 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 2, 2019 at 9:36 YogaYoga 9192 gold badges20 silver badges39 bronze badges 1
  • 2 And when/where do you want to obtain that information, because this is important...? – Krzysiek Dróżdż Commented Apr 2, 2019 at 9:40
Add a comment  | 

2 Answers 2

Reset to default 0

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 }

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

$postType = get_query_var( 'post_type' );
if(file_exists('archive-'.$postType.'.php')){
    // You have the specific archive file for the post type
}else{
    // You don't have the specific archive file for the post type
}

本文标签: pluginsHow to get current template file used by WordPress