将 WordPress 主题模板文件移动到子目录

Mos*_*she 5 php wordpress wordpress-theming

我想重组我正在创建的 WordPress 主题中的模板文件。现在,诸如single.php和 之类的文件archive.php位于我的主题的根级别。我想将它们移动到自己的文件夹中 - 比如说一个名为pages. 所以它看起来像这样:

mytheme 
  -- pages
      -- archive.php
      -- single.php
  -- functions.php
  -- index.php
  -- style.css
Run Code Online (Sandbox Code Playgroud)

这可能吗?如果是这样,怎么办?

谢谢。

小智 4

您可以使用single_template过滤器和{$type}_template过滤器进行存档、类别等。

我认为您正在寻找这样的东西:

function get_new_single_template( $single_template ) {
  global $post;
    $single_template = get_stylesheet_directory() . '/pages/single.php';
  return $single_template;
}
add_filter( 'single_template', 'get_new_single_template' );

function get_new_archive_template( $archive_template ) {
  global $post;
    $archive_template = get_stylesheet_directory() . '/pages/archive.php';
  return $archive_template;
}
add_filter( 'archive_template', 'get_new_archive_template' );
Run Code Online (Sandbox Code Playgroud)

这将转到您的functions.php