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
Check out this Alternative
<?php
// 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
}
?>