wordpress学习三:wordpress自带的模板学习
在《学习二》里,大概说了下怎么去查找模板,本节我们以一个简单的模板为例子,继续说说wordpress的模板机制,看看做一个自己的模板需要哪些知识点。
页面模板渲染
wordpress的模板位于wp-content/themes目录下,wordpress可以自动加载新增的模板目录。通过上一节的简单介绍,知道wordpress默认打开时会查找home.php或者index.php。我们先看看wordpress自带的模板twentyfifteen的index.php页面。
<?php get_header(); ?> <div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?> <?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?> <?php twentythirteen_paging_nav(); ?> <?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?> </div><!-- #content -->
</div><!-- #primary --> <?php get_sidebar(); ?>
<?php get_footer(); ?>
这个index.php里的代码很简单,只描述了怎么去展示首页的逻辑。代码中有get_header(), get_sidebar(), get_footer()三个加载函数,分别加载页面的头部,尾部和侧边栏。一般来说,网站的这三个部分是公用的,所以wordpress抽象了三个函数,来实现代码的重用。以 get_sidebar()的代码为例,看看是如何加载页面的不同部分的。
function get_sidebar( $name = null ) {
/**
* Fires before the sidebar template file is loaded.
*
* The hook allows a specific sidebar template file to be used in place of the
* default sidebar template file. If your file is called sidebar-new.php,
* you would specify the filename in the hook as get_sidebar( 'new' ).
*
* @since 2.2.0
* @since 2.8.0 $name parameter added.
*
* @param string $name Name of the specific sidebar file to use.
*/
do_action( 'get_sidebar', $name ); $templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "sidebar-{$name}.php"; $templates[] = 'sidebar.php'; // Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/sidebar.php');
}
这个函数默认加载的是sidebar.php文件,也可以传入不同的$name,来加载sidebar-{$name}.php类型的php文件,实现不同的页面可以load不同的侧边栏,头部和尾部的代码与侧边栏的代码类似。wordpress中模板的php文件加载均采用了类似的罗辑。
除了加载模板的php文件,index.php中还调用了wordpress中定义的一些函数,下面我们看看这些函数是 干什么的。
function have_posts() {
global $wp_query; return $wp_query->have_posts();
}
$wp_query是在初始化时创建的WP_Query对象;
$GLOBALS['wp_query'] = new WP_Query();
在wp-setting.php中执行。该对象的have_posts()返回当前的url查询罗辑下是否还是需要显示的post。
the_post()将全局的$post对象设置为下一个选择的post。
function get_template_part( $slug, $name = null ) {
/**
* Fires before the specified template part file is loaded.
*
* The dynamic portion of the hook name, `$slug`, refers to the slug name
* for the generic template part.
*
* @since 3.0.0
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialized template.
*/
do_action( "get_template_part_{$slug}", $slug, $name ); $templates = array();
$name = (string) $name;
if ( '' !== $name )
$templates[] = "{$slug}-{$name}.php"; $templates[] = "{$slug}.php"; locate_template($templates, true, false);
}
上面的代码是一个通用的加载模板php文件的函数,类似于get_header,但是比他们更通用。通过槽位和name的限定,来实现更灵活的加载模板。
get_template_part( 'content', get_post_format() );
上面这行代码会加载 content.php模板文件,其中包含了一个post显示的html代码
<?php
/**
* The default template for displaying content
*
* Used for both single and index/archive/search.
*
* @package WordPress
* @subpackage Twenty_Thirteen
* @since Twenty Thirteen 1.0
*/
?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() && ! is_attachment() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?> <?php if ( is_single() ) : ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else : ?>
<h1 class="entry-title">
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
</h1>
<?php endif; // is_single() ?> <div class="entry-meta">
<?php twentythirteen_entry_meta(); ?>
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-meta -->
</header><!-- .entry-header --> <?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( 'Continue reading %s <span class="meta-nav">→</span>', 'twentythirteen' ),
the_title( '<span class="screen-reader-text">', '</span>', false )
) ); wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) );
?>
</div><!-- .entry-content -->
<?php endif; ?> <footer class="entry-meta">
<?php if ( comments_open() && ! is_single() ) : ?>
<div class="comments-link">
<?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a comment', 'twentythirteen' ) . '</span>', __( 'One comment so far', 'twentythirteen' ), __( 'View all % comments', 'twentythirteen' ) ); ?>
</div><!-- .comments-link -->
<?php endif; // comments_open() ?> <?php if ( is_single() && get_the_author_meta( 'description' ) && is_multi_author() ) : ?>
<?php get_template_part( 'author-bio' ); ?>
<?php endif; ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
content.php是页面里一个post的显示模板,排版什么的我们先不关心, 看看里面用到的wordpress的函数。这些函数主要在post-template.php中
- the_ID(): echo当前post的id
- post_class(): Display the classes for the post div.
- the_title: Display or retrieve the current post title with optional content.
- the_permalink():Display the permalink for the current post.
- the_excerpt():Display the post excerpt.
- the_content(): Display the post content.
只列出了几个要紧的函数,wordpress就是通过这些一个个的代码片段,最终拼接出一个完整的显示页面。
页面的数据获取
上面主要说了在获取数据后,如何用数据拼接成一个完整的页面。继续来看看数据是如何获取的。
wordpress在query.php文件中定以了一个WP_Query类,这个类负责对GET和POST请求的参数进行解析,维护在一个页面显示周期里所需要的数据读取。封装了基本的数据读取工作。
主要的数据读取在WP_Query的 get_posts()函数中,该函数有一千多行,主要就是利用$wpdb从数据库读取数据,封装到对应的POST对象中。get_posts()函数可能在 在WP_Query构造时调用,主要依赖于是否传入了查询字符串。
public function __construct($query = '') {
if ( ! empty($query) ) {
$this->query($query);
}
}
public function query( $query ) {
$this->init();
$this->query = $this->query_vars = wp_parse_args( $query );
return $this->get_posts();
}
WP_Query的 init函数主要建立一个干净的变量环境。get_posts函数中有大量的各种参数的解析罗辑。
WP类的main函数是调用页面数据的入口。其中会建立查询字符串,然后调用上面的query函数来检索相应的post。打开首页是传入的查询字符串对象为空。
函数get_posts中会按照查询参数,拼接处要使用的sql语句,代码很长,杂,逻辑比较多。拼好字符串后,在用$wpdb这个wpdb类实例(wp-db.php文件中)来进行数据库操作。这个类中封装了读取mysql的代码。具体的文档可以参看
https://codex.wordpress.org/Function_Reference/wpdb_Class
这个文章我大概看了下,主要是对mysql的一个简单封装。
wordpress学习三:wordpress自带的模板学习的更多相关文章
- spring源码学习(三)--spring循环引用源码学习
在spring中,是支持单实例bean的循环引用(循环依赖)的,循环依赖,简单而言,就是A类中注入了B类,B类中注入了A类,首先贴出我的代码示例 @Component public class Add ...
- Scala学习笔记2 (带着问题学习, 逐渐扩展。理解吃透scala.)
问题: 把 文本字符串"[1, 2, 3, 4, 5]" 转换成一个数组. 答案: val x = "[1, 2, 3, 4, 5]" val y =x sli ...
- WordPress不同分类使用不同的文章模板
倡萌昨天分享的 Custom Post Template 和 Single Post Template 可以让你自定义每篇文章的文章模板,今天来说说WordPress不同分类使用不同的文章模板. 方法 ...
- XTemplate模板学习和使用总结
XTemplate模板学习和使用总结 前言 XTemplate是我接触的第一个模板语言,用在公司的一个NodeJS项目中,跟它打交道经常是因为需要使用它的语法向模板中注入数据.因为是刚入门前端不久 ...
- Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板
原文:Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...
- 怎么用MindManager自带的模板和设计画思维导图
小编知道大家平时工作学习都很忙,思维导图能完成的效率越高越好.所以今天,小编就为大家介绍两个能高效使用思维导图软件完成制作思维导图的小技巧.保证内容充实美观,还不费时间. 一.使用模板 打开MindM ...
- HTTP学习三:HTTPS
HTTP学习三:HTTPS 1 HTTP安全问题 HTTP1.0/1.1在网络中是明文传输的,因此会被黑客进行攻击. 1.1 窃取数据 因为HTTP1.0/1.1是明文的,黑客很容易获得用户的重要数据 ...
- Django学习之django自带的contentType表 GenericRelation GenericForeignKey
Django学习之django自带的contentType表 通过django的contentType表来搞定一个表里面有多个外键的简单处理: 摘自:https://blog.csdn.net/a ...
- DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件 本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...
随机推荐
- Android 7.0以上版本 系统解决拍照的问题 exposed beyond app through ClipData.Item.getUri()
解决方案1: android.os.FileUriExposedException: file:///storage/emulated/0/ilive/images/photophoto.jpeg e ...
- [JSOI2008]星球大战starwar
嘟嘟嘟 维护联通块自然想到并查集,然而题中说是删边,不是很好做,因此我们可以离线下来然后倒序操作,就变成了添加边的同时维护联通块数量. 首先我们把k次打击后剩的边都添加到图中,表示倒序时的初始状态.然 ...
- PAT B1040 有几个PAT (25 分)
字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位(T). 现 ...
- redis集群遇到的坑
[root@insure src]# ./redis-cli -c -h 172.16.*.* -p 6370 输入密码: auth 密码 查看节点信息 172.16.*.*:6370> clu ...
- CentOS 6.8 虚拟机安装详解
第一步:安装 VMware 官方网站:www.vmware.com 下载百度云链接:http://pan.baidu.com/s/1bphDOWv 密码:0zix VMware 是一个虚拟 PC 的软 ...
- Swift图书展示项目笔记
1.Swift语言特点 Extensions(扩展):就是向一个已有的类.结构体.枚举类型或者协议类型添加新功能.这包括在没有权限获取原始源代码的情况下扩展类型的能力(即逆向建模) map: 得到一个 ...
- odoo明细表汇总数据
一.在主表中#改动地方 总结算金额 求和:def _get_subtotal2(self, cr, uid, ids, field_name, arg, context=None): # 初始化 re ...
- 【转】Google Chrome中顺时针/逆时针滚动圆的含义
当浏览器处于以下状态时,看起来好像圆圈是逆时针滚动的: 解析主机名 连接服务器 等待响应(从服务器发送第一个字节之前?) 当浏览器处于以下状态时,圆圈似乎顺时针滚动: 加载页面或引用的资源 在标签页中 ...
- 大数据入门第十四天——Hbase详解(二)基本概念与命令、javaAPI
一.hbase数据模型 完整的官方文档的翻译,参考:https://www.cnblogs.com/simple-focus/p/6198329.html 1.rowkey 与nosql数据库们一样, ...
- Android“寄生兽”漏洞技术分析
一.关于app的缓存代码 安卓的应用程序apk文件是zip压缩格式的文件,apk文件中包含的classes.dex文件相当于app的可执行文件,当app运行后系统会对classes.dex进行优化,生 ...