I need to so some stuff only when specific templates (homepages) are used, I couldn't think of any better solution than this since is_page_template() do not handle arrays:

        $home_1 = is_page_template('home-1.php');
        $home_2 = is_page_template('home-2.php');
        $home_3 = is_page_template('home-3.php');
        $home_4 = is_page_template('home-4.php');
        $home_5 = is_page_template('home-5.php');
        $home_6 = is_page_template('home-6.php');

        if ( $home_1 || $home_2 || $home_3 || $home_4 || $home_5 || $home_6  ) {
             //Do stuff

Since I need this for several parts, I was wondering if there is any cleaner way to do this not to repeat the same code throughout template files? Maybe inside a function in functions.php? Thanks

2 Answers 2


Check out this Alternative

  // in the loop:
  $template = get_page_template_slug( get_the_ID() );
  if (in_array($template,array('home1','home2')) ) ){
     // Yep, Do your stuff

  // anywhere:
  $template = get_page_template_slug( $some_post_ID );
  if (in_array($template,array('home1','home2')) ) ){
     // Yep, Do your stuff

is_page_template() does support arrays! Here's an example:

if ( is_page_template( array(
) ) )  
    //Do stuff

Source: https://developer.wordpress.org/reference/functions/is_page_template/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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