php – 子目录中的WordPress模板文件
不久前我开始了一个新的Wordpress项目.但是我遇到了一个问题.由于有多种设计,我需要为不同的页面模板上的页面,帖子和文本格式输出创建多个模板.
因为我是这么多的模板文件,我想创建一些子目录.我知道,自从Wordpress 3.4及更高版本,您就可以使用所有页面模板的子目录名称页面模板,但我怎样才能将其用于格式文件和帖子文件. 是的我确实尝试添加以下功能: get_template_part('/template-parts/page-templates','page'); 和 require( get_template_directory() . '/template-parts/template-tags.php' ); 我想创建的理想目录结构如下: wp-content/themes/mytheme - archive - 404 - CSS - JS - Images - template-parts (dir) -- page-templates (dir for page-template files.) -- format-templates (dir for format-templates.) -- post-templates (dir for post-templates.) - header - footer 所以要清楚.我想为上面的模板文件创建结构.不要介意像CSS等文件夹.这些设置正确.在我成功创建结构之后,目的是能够从/ wp-admin编辑页面部分选择模板,如页面模板. 解决方法
WP_Theme类包含提供此过滤器的方法get_page_templates():
apply_filters(“theme _ {$post_type} _templates”,$post_templates,$this,$post,$post_type); line 1103 of class-wp-theme.php 请记住,在wordpress帖子和页面是 add_filter(“theme_post_templates”,…和add_filter(“theme_page_templates”,… Under the codex’s “Used By” section对于该方法,它指出:
这让我相信它会在/ wp-admin / edit页面部分提供它们. 有关过滤器的核心文件的信息: * Filters list of page templates for a theme. * * The dynamic portion of the hook name,`$post_type`,refers to the post type. * @since 4.7.0 Added the `$post_type` parameter. * * @param array $post_templates Array of page templates. Keys are filenames,* values are translated names. * @param WP_Theme $this The theme object. * @param WP_Post|null $post The post being edited,provided for context,or null. * @param string $post_type Post type to get the templates for. 我不确定相对uri是否在这里工作,或者你是否需要get_theme_file_path(),但我假设前者. function deep_templates( $post_templates,$theme,$post_type ) $post_templates['/folder/folder/folder/file.php'] = "Page Style One"; $post_templates['/folder/other-folder/file.php'] = "Page Style Two"; return $post_templates; add_filter( 'theme_page_templates','deep_templates' ); AND / OR add_filter( 'theme_post_templates','deep_templates' ); add_filter( 'theme_my-custom-cpt_templates','deep_templates' ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |