您现在的位置是:首页 > 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输出网站文件的方法
通过ftp登录网站目录,把文件 基本下载下来即可。如果要导出整站,还要到数据库把数据库导出来。在本地运行的时候导入数据库,然后修改数据库配置文件就可以了。
Wordpress常用的插件介绍
留言相关插件:SEO相关插件:互动相关插件:Author Complete Post List:展示某个特定作者的 基本文章
wordpress-4.4.1数据库表结构解析
wordpress-4.4.1.zip 安装包SQL结构 :wp_commentmeta :文章评论额外信息表。CREATE TABLE IF NOT EXISTS `wp_commentmeta` (`meta_id` bigint(20) unsigned NOT NULL A
linux系统安装WordPress的方法
WordPress是一种使用PHP语言和MySQL数据库开发的开源、免费的Blog(博客,网志)引擎,用户可以在支持PHP和MySQL数据库的服务器上建立自己的Blog。WordPress是一个功能非常强大的博客系统
相关源码
-
(自适应响应式)电子数码科技产品介绍带留言网站模板下载为电子产品企业设计的展示系统,集成智能产品对比器、参数规格表和展示模块。支持消费电子、智能设备等多级分类展示,内置产品技术参数数据库。查看源码 -
帝国CMS游戏应用APP推广下载站模板免费下载本模板为移动应用推广、手机游戏推广行业设计,集成H5游戏平台与APP下载功能,支持PC端与移动端自适应访问。专注于为应用开发商、游戏发行商提供专业的线上推广展示平台。查看源码 -
(自适应)工商代理公司注册财务会计pbootcms模板免费下载为工商代理、财务会计服务企业设计的网站模板,基于PbootCMS开发,充分考虑了企业服务行业的展示需求与客户转化路径。设计风格专业严谨,布局清晰合理,呈现企业服务内容与专业优势。查看源码 -
自适应新闻资讯技术博客个人网站pbootcms模板该模板基于PbootCMS开源内核深度开发,该模板适用于游戏新闻网站、游戏博客等企业或个人网站,新闻资讯技术博客pbootcms模板;采用六级分辨率断点适配技术查看源码 -
(自适应)WordPress主题SEO自媒体博客资讯模板RabbitV2.0Rabbit v2.0主题专注于网站搜索引擎优化需求,为博客、自媒体及资讯类网站提供专业的SEO技术解决方案。该主题从架构设计到功能实现均围绕搜索引擎优化理念展开。查看源码 -
(PC+WAP)楼承板建筑工程合金钢铁材料pbootcms网站源码下载为楼承板生产商及建筑工程企业设计的响应式网站模板,采用PbootCMS内核开发,同步适配电脑与移动设备浏览。通过模块化结构展示工程案例、产品参数等专业内容,帮助建筑建材企业建立标准化数字展示平台,提升行业专业形象。查看源码
| 分享笔记 (共有 篇笔记) |

