php – 子目录中的WordPress模板文件

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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帖子和页面post_types,

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' );

脚本宝典总结

以上是脚本宝典为你收集整理的php – 子目录中的WordPress模板文件全部内容,希望文章能够帮你解决php – 子目录中的WordPress模板文件所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。