更清晰地为IS_PAGE_TEMPLATE()定义多个变量

时间:2014-07-18 作者:djwd

我只需要在使用特定模板(主页)时使用一些东西,我想不出比这更好的解决方案,因为is\\u page\\u template()不处理数组:

        $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
        }
由于我在几个部分都需要这个,我想知道是否有更干净的方法来做到这一点,不在整个模板文件中重复相同的代码?可能在函数中的函数内部。php?谢谢

1 个回复
SO网友:Rajeev Vyas

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
  }
?>
结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register