最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - How to get current template file used by WordPress?

matteradmin6PV0评论

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
}
Post a comment

comment list (0)

  1. No comments so far