您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程
wordpress文件template-loader.php代码说明
寻雁2025-03-28WordPress教程已有人查阅
导读再看template-laoder.php,这个文件总共只有45行。它的作用是基于访问的URL装载正确的模板,文件第六行,也是第一条语句,如下:
再看template-laoder.php,这个文件总共只有45行。它的作用是基于访问的URL装载正确的模板,文件第六行,也是第一条语句,如下:
‘template_redirect’的Action(动作)在include下的文件中出现。除了这里的do_action外,还有其他三个文 件中:default-filters.php,和ms-default-filters.php中以及canonical.php中出现。不包括wp- content目录下面出现的。
接下来是个判断语句;
do_robots函数位于functions.php1779行。作用:显示robot.txt的内容。
然后是个大的if语句:
以get_index_template为例:作用在当前或父模板下检索index模板路径,位置在theme.php725行。function get_index_template() { return get_query_template('index'); }
在这个函数里面,就把主题的模板给装载进来了,如何操作的?马上来~~
if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) do_action('template_redirect');
首先判断是否使用Themes,这个WP_USE_THEMES常量在index.php中第一句就被设置为true。因此条件成立,会执行do_action(‘template_redirect’)语句。do_action('template_redirect')都做了什么?wordpress默认的有如下:‘template_redirect’的Action(动作)在include下的文件中出现。除了这里的do_action外,还有其他三个文 件中:default-filters.php,和ms-default-filters.php中以及canonical.php中出现。不包括wp- content目录下面出现的。
canonical.php (411行): add_action(‘template_redirect’, ‘redirect_canonical’);
default-filters.php(200行): add_action( ‘template_redirect’, ‘wp_shortlink_header’,11, 0 );
default-filters.php(243行): add_action( ‘template_redirect’, ‘wp_old_slug_redirect’);
ms-default-filters.php(32行): add_action( ‘template_redirect’, ‘maybe_redirect_404′ );
ms-functions.php(1353行): add_action( ‘template_redirect’, ‘redirect_mu_dashboard’ );
default-filters.php文件设置了wordpress大部分默认的filter和action。文件内容都是诸如下面的形式。
add_action( ‘xxx’,'xxxx’);
ms-default-filters.php是多站点时候设置的,内容类似default-filters.php。wordpress默认情况下没有开启多站点。如果需要开启,请按照wordpress的指导文件操作。
add_action( 'template_redirect', 'wp_shortlink_header',11, 0)
wp_shortlink_header位于文件link-template.php 2230行。作用是如果当前页面定义了短连接,就在header中发送个短链接.形容:<link rel='shortlink' href='http://localhost/?p=1234' />这样的缩短网址。
add_action( 'template_redirect', 'wp_old_slug_redirect');
wp_old_slug_redirect() 在query.php2807行,slug是什么?slug是url的一部分。在wordpress后台的长久链接设置(/wp-admin /options-permalink.php)里,用户可以自定义链接格式。绝大多数自定义的用户喜欢在url中包含由文章标题生成的一串字符,这一串 字符就是post slug。这个函数功能是重定向old slug到正确的链接。接下来是个判断语句;
// Process feeds and trackbacks even if not using themes. if ( is_robots() ) : do_action('do_robots'); return; elseif ( is_feed() ) : do_feed(); return; elseif ( is_trackback() ) : include( ABSPATH . 'wp-trackback.php' ); return; endif;
is_robots函数判断当前查询是否是robot。位于query.php491行。do_robots函数位于functions.php1779行。作用:显示robot.txt的内容。
然后是个大的if语句:
if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) : $template = false; if ( is_404() && $template = get_404_template() ) : elseif ( is_search() && $template = get_search_template()) : elseif ( is_tax() && $template = get_taxonomy_template()) : elseif ( is_front_page() && $template = get_front_page_template()) : elseif ( is_home() && $template = get_home_template()) : elseif ( is_attachment() && $template = get_attachment_template()) : remove_filter('the_content', 'prepend_attachment'); elseif ( is_single() && $template = get_single_template()) : elseif ( is_page() && $template = get_page_template()) : elseif ( is_category() && $template = get_category_template()) : elseif ( is_tag()&& $template = get_tag_template()) : elseif ( is_author()&& $template = get_author_template()) : elseif ( is_date()&& $template = get_date_template()) : elseif ( is_archive()&& $template = get_archive_template()) : elseif ( is_comments_popup() && $template = get_comments_popup_template() ) : elseif ( is_paged()&& $template = get_paged_template() ) : else : $template = get_index_template(); endif; if ( $template = apply_filters( 'template_include', $template ) ) include( $template ); return; endif;
这个大if语句中形如get_xxxx_template()的函数都定义在theme.php中。以get_index_template为例:作用在当前或父模板下检索index模板路径,位置在theme.php725行。function get_index_template() { return get_query_template('index'); }
在这个函数里面,就把主题的模板给装载进来了,如何操作的?马上来~~
本文标签:
很赞哦! ()
相关教程
图文教程
wordpress主页修改为home的方法
具体更改步骤如下:登录使用wordpress建站的后台,进入后台页面。点击右侧外观中的菜单。
Wordpress把选中内容分享到新浪微博的实现方法
本文实例讲述了Wordpress将选中内容分享到新浪腾讯微博的方法。分享给大家供大家参考。具体方法如下:
wordpress主题开发难不难,wordpress运行条件和本地环境说明
wordpress主题开发不难,但是想要实现wordpress主题开发,在你还需要了解以下内容:
WordPress不同分类使用不同列表样式的实现方法
方法一:创建独立ID或别名的样式文件WordPress默认的分类是查找对应ID的主题文件,找不到就会指向archive.php文件,如果archive.php文件不存在,就会默认使用index.php文件。 到博
相关源码
-
自适应黑色简繁双语轴承齿轮机械设备制造网站模板该模板为轴承齿轮机械制造企业提供一体化网站建设方案,着重解决行业特有的多语言展示、移动端适配和高效率内容管理需求,帮助企业精准展示产品特性与工艺流程查看源码 -
(自适应)餐具英文外贸生活用品带下载功能网站模板免费下载为餐具及生活用品外贸企业打造的响应式网站模板,基于PbootCMS内核开发。突出产品展示与多语言支持特性,通过自适应设计确保更好客户在手机、平板、电脑等设备上获得一致浏览体验。查看源码 -
自适应建材瓷砖卫浴大理石类pbootcms网站模板源码下载为建材瓷砖、卫浴瓷砖企业打造的高端响应式门户模板,基于PbootCMS内核深度开发。采用前沿HTML5自适应架构,无缝兼容手机端触控交互与PC端展示场景。查看源码 -
(PC+WAP)蓝色新能源环保太阳能光伏模板pbootcms下载新能源环保行业网站解决方案为太阳能光伏系统企业打造的专业网站模板,助力新能源企业高效展示产品与服务PbootCMS内核开发,源码开放可修改PC与手机端自适应设计查看源码 -
(自适应响应式)蓝色勘察设计院机构单位商会协会pbootcms模板下载本模板基于PbootCMS系统开发,为勘察设计院所、工程咨询单位设计,特别适合展示设计成果、技术服务和项目案例。采用响应式技术,确保设计图纸和方案以及文案在不同设备上都能清晰展示。查看源码 -
(自适应)物流运输快递仓储货运网站模板免费下载基于PbootCMS内核开发的物流运输行业专用模板,深度适配仓储货运企业的业务展示需求。前端采用响应式布局,自动适配手机端访问,后台数据实时同步更新,帮助企业高效展示运输网络、仓储设施、服务流程等核心业务模块。查看源码
| 分享笔记 (共有 篇笔记) |

