修改函数以仅返回文件名中带有“home”的结果


Modify function to only return results with "home" in their filename

>我有以下自定义页面模板文件名:

  • 模板关于1.php
  • 模板关于2.php
  • 模板主页-1.php
  • 模板-主页-2.php

我有这个函数来获取页面模板:

function get_page_templates_select() {
 $teh_cats = get_page_templates();
 foreach ( $teh_cats as $template_name => $template_filename ) {
     $results[] = $template_name;
   }
   return $results;
}

如何修改函数以仅返回文件名中包含"home"的模板?

想了想就想通了。.

if (stripos(strtolower($template_filename), 'home') !== false) {
    $results[] = $template_name;
}

我想你可以使用:

function get_page_templates_select() {
 $teh_cats = get_page_templates();
 foreach ( $teh_cats as $template_name => $template_filename ) {
     if (preg_match('/home/i', $template_filename)) {
         $results[] = $template_name;
     }
   }
   return $results;
}