typecho模板制作快速入门

news/2024/10/22 9:40:19/

模板制作快速入门

模板的制作并非难事,只要你写好了HTML和CSS,嵌套模板就非常简单了,你无需了解标签的内部结构,你只要会使用,模板就能迅速完成。这篇文章只简单的介绍了常用标签的使用方法,希望能带你进入模板的世界。

本篇文章以Typecho的默认模板为例,您可以打开默认模板default边看边学习。该模板所在的路径为/usr/themes/default 进入该目录后,我们可以看到有许多文件,别犯愁,我们将在后续的内容中一一进行介绍,所有在当前目录下的文件都能在后台的模板编辑页面进行编辑。

index.php 主页模板

模板信息
我们先从主文件说起,打开这个文件,首先看到的是注释:

/*** 这是typecho系统的一套默认皮肤。你可以在<a href="http://typecho.org">typecho的官方网站</a>获得更多关于此皮肤的信息* * @package Typecho Default Theme * @author typecho* @version 1.0.0* @link http://typecho.org*/

这是模板信息存放的地方,它将在后台都模板选择页显示。前两行是简短的介绍,每个“*”表示一个段落。@package 表示模板名,@author 表示作者名,@version 是模板的版本号,@link 是作者的网站连接。

紧挨着注释下方的 include('header.php'),在结尾处也会看到 include('sidebar.php')include('footer.php')。这些语句用来调用模板的其它模块。

header故名思议是页首,sidebar是侧栏,footer是页脚。

显示文章列表

<?php while($this->next()): ?><div class="post"><h2 class="entry_title"><a href="<?php $this->permalink() ?>"><?php $this->title() ?></a></h2><div class="entry_data">Published by <a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a> on <?php $this->date('F j, Y'); ?> in <?php $this->category(','); ?>.<?php $this->commentsNum('%d Comments'); ?>.</div><div class="entry_text"><?php $this->content('Continue Reading...'); ?></div></div>
<?php endwhile; ?>

进入文章循环,输出文章,剥开html代码,一句一句介绍

文章所在的连接

<?php $this->permalink() ?>

文章标题

<?php $this->title() ?>

文章作者

<?php $this->author(); ?>

文章作者地址

<?php $this->author->permalink(); ?>

文章的发布日期,格式可参考PHP日期格式

<?php $this->date('F j, Y'); ?>

文章所在分类

<?php $this->category(','); ?>

文章评论数及连接

<?php $this->commentsNum('%d Comments'); ?>

文章内容,其中的“Continue Reading…”是显示摘要时隐藏部分的邀请连接

<?php $this->content('Continue Reading...'); ?>

好了,文章显示结束,别忘了结束循环。

文章分页

<?php $this->pageNav(); ?>

文章输出结束后别忘了增加分页,至此,index.php的常见内容结束,应该不糊涂吧。

header.php 通用头部模板

html编码设置

打开这个文件,见到的第一个php代码就是:

<meta http-equiv="content-type" content="text/html; charset=<?php $this->options->charset(); ?>" />

调用默认的编码,现在最经常用的大都是utf-8吧。所以我通常是直接写成utf-8,省去php处理时间。

页面标题

<title><?php $this->options->title(); ?><?php $this->archiveTitle(); ?></title>

通常情况下直接复制使用,如果你没有时间的话。

导入样式

<link rel="stylesheet" type="text/css" media="all" href="<?php $this->options->themeUrl('style.css'); ?>" />

其中style.css是样式文件相对模板目录的路径和文件名。

其它HTML头部信息

别忘了这句,它关系到RSS信息、客户端程序以及插件的正常使用。

页面导航

<ul class="clearfix" id="nav_menu"><li><a href="<?php $this->options->siteUrl(); ?>">Home</a></li><?php $this->widget('Widget_Contents_Page_List')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
</ul>

本处使用了无序列的页面列表,其中{permalink}是页面的地址,{title}是页面的标题。

网站名称

<h1><a href="<?php $this->options->siteUrl(); ?>"><?php $this->options->title() ?></a></h1>
<span><?php $this->options->description() ?></span>

站点地址,对应用后台的站点地址

<?php $this->options->siteUrl(); ?>

网站名称,对应用后台的站点名称

<?php $this->options->title() ?>

站点描述,对应用后台的站点描述

<?php $this->options->description() ?>

站点关键词,对应用后台的关键词

<?php $this->options->keywords() ?>

站内搜索

<form method="post" action=""><div><input type="text" name="s" class="text" size="32" /> <input type="submit" class="submit" value="Search" /></div>
</form>

当你的文章很多很多,这个搜索就必不可少。

sidebar.php 通用边栏

最新文章列表

<ul><?php $this->widget('Widget_Contents_Post_Recent')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
</ul>

获取最新的10篇文章标题,得到的html是

<ul><li><a href="http://example.com/2008/12/31/sample-post-one">文章1的标题</a></li><li><a href="http://example.com/2008/12/31/sample-post-two">文章2的标题</a></li><!-- 省略n个重复 --><li><a href="http://example.com/2008/12/31/sample-post-ten">文章10的标题</a></li>
</ul>

最新回复列表

<ul><?php $this->widget('Widget_Comments_Recent')->to($comments); ?><?php while($comments->next()): ?><li><?php $comments->author(false); ?>: <a href="<?php $comments->permalink(); ?>"><?php $comments->excerpt(10, '[...]'); ?></a></li><?php endwhile; ?>
</ul>

获取最新的10个回复,得到的html是

<ul><li>回复人名字: <a href="http://example.com/2008/12/31/sample-post#comments-12">回复的内容[...]</a></li><li>回复人名字: <a href="http://example.com/2008/12/31/sample-post#comments-11">回复的内容[...]</a></li><!-- 省略n个重复 -->
</ul>

其中<?php $comments->excerpt(10, '[...]'); ?>,“10”代表要回复内容截取的字的个数,“[…]”代表省略的意思,你可以自行修改。

文章分类列表

<ul><?php $this->widget('Widget_Metas_Category_List')->parse('<li><a href="{permalink}">{name}</a> ({count})</li>'); ?>
</ul>

输出:

<ul><li><a href="http://example.com/category/uncategories">Uncategories</a>(10)</li><li><a href="http://example.com/category/category-1">Category-1</a>(2)</li>
</ul>

其中{count}是获取该分类下的文章总数量。

按月归档

<ul><?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')->parse('<li><a href="{permalink}">{date}</a></li>'); ?></ul>

输出:

<ul><li><a href="http://example.com/2008/11">November 2008</a></li><li><a href="http://example.com/2008/10">October 2008</a></li>
</ul>

其它连接

<ul><?php if($this->user->hasLogin()): ?><li class="last"><a href="<?php $this->options->index('Logout.do'); ?>">Logout (<?php $this->user->screenName(); ?>)</a></li><?php else: ?><li class="last"><a href="<?php $this->options->adminUrl('login.php'); ?>">Login</a></li><?php endif; ?>
</ul>

这些是可有可无的,只是为了方便登录登出。 边栏如果采用默认模板,那么可以在后台设置中的进行便捷修改,具体方法:控制台->设置外观->设置外观

footer.php 底部文件或是脚部文件

页脚文件,推荐大家把一些较大的js放在这个文件中最后载入,不会影响阅读。看看我们的footer要讲解些什么?

<a href="<?php $this->options->feedUrl(); ?>">Entries (RSS)</a> <!-- 文章的RSS地址连接 -->
<a href="<?php $this->options->commentsFeedUrl(); ?>">Comments (RSS)</a>. <!-- 评论的RSS地址连接 -->

另外别忘了添加

<a href="http://typecho.org">Typecho</a>

以示对Typecho的支持,简单吧。
可以更好的帮助 typecho 发扬光大,扬眉吐气。
到目前为至,你已完成了75%的嵌套。

post.php 文章页

post页和index是差不多的,但是我们还是要说一下不同之处。

Tag 标签

Tags: <?php $this->tags(',', true, 'none'); ?>

这是获取当前单篇文章的tags标签,用“,”符号隔开。

调用评论页

<?php include('comments.php'); ?>

comments.php 内嵌评论页

评论列表

<h4><?php $this->commentsNum('No Response', 'One Response to"' . $this->title . '"', '%d Responses to "' . $this->title . '"'); ?></h4>
<ol id="comment_list"><?php $this->comments()->to($comments); ?><?php while($comments->next()): ?><li id="<?php $comments->theId(); ?>"><div class="comment_data"><?php echo $comments->sequence(); ?>. <strong><?php $comments->author(); ?></strong>on <?php $comments->date('F jS, Y'); ?> at <?php $comments->date('h:i a'); ?></div><div class="comment_body"><?php $comments->content(); ?></div></li><?php endwhile; ?>
</ol>

还是循环输出评论:
每个评论的唯一ID

<?php $comments->theId(); ?>

评论所在楼层

<?php $comments->sequence(); ?> 	

回复地址

<?php $comments->responseUrl(); ?> 	

回复框ID

<?php $comments->responseId(); ?> 	

trackback地址

<?php $comments->trackbackUrl(); ?> 	

评论者的名字

<?php $comments->author(); ?> 	

评论日期

<?php $comments->date('F jS, Y'); ?> 	

评论时间

<?php $comments->date('h:i a'); ?> 	

评论内容

<?php $comments->content(); ?> 	

结束循环。我们用有序列表<ol>,因为评论的发表是有先后顺序的。

评论输入表单

<!-- 判断设置是否允许对当前文章进行评论 -->
<?php if($this->allow('comment')): ?><h4 id="response">Leave a Reply</h4><!-- 输入表单开始 --><form method="post" action="<?php $this->commentUrl() ?>" id="comment_form"><!-- 如果当前用户已经登录 --><?php if($this->user->hasLogin()): ?><!-- 显示当前登录用户的用户名以及登出连接 --><p>Logged in as <a href="<?php $this->options->adminUrl(); ?>"><?php $this->user->screenName(); ?></a>. <a href="<?php $this->options->index('Logout.do'); ?>" title="Logout">Logout &raquo;</a></p><!-- 若当前用户未登录 --><?php else: ?><!-- 要求输入名字、邮箱、网址 --><p><input type="text" name="author" class="text" size="35" value="<?php $this->remember('author'); ?>" /><label>Name (Required)</label></p><p><input type="text" name="mail" class="text" size="35" value="<?php $this->remember('mail'); ?>" /><label>E-mail (Required *will not be published)</label></p><p><input type="text" name="url" class="text" size="35" value="<?php $this->remember('url'); ?>" /><label>Website</label></p><?php endif; ?><!-- 输入要回复的内容 --><p><textarea rows="10" cols="50" name="text"><?php $this->remember('text'); ?></textarea></p><p><input type="submit" value="Submit Comment" class="submit" /></p></form>
<?php endif; ?>

很多情况下并不对评论文件进行修改,可以直接拿来使用写入相应的css。

page.php 单页面模板

页面的显示方式,通常情况下和 single.php 无差别。(官方文档没有详细介绍,后续会进行实际应用的扩展)

archive.php 文章列表展示页模板

显示某分类下的文章列表、搜索结果列表显示时调用的文件。(官方文档没有详细介绍,后续会进行实际应用的扩展)

结束语

这是一篇基于官方的文档做的模板制作快速入门,在官方文档的基础做了一些调整,这些调整可以更加轻松的帮助阅读者理解并且应用。如果在实际应用的过程中,有任何问题都可以随时互动。互动的方式有两种,私信或评价


http://www.ppmy.cn/news/77048.html

相关文章

PaperMoon开发者关系工程师(中国)招聘

PaperMoon是一家全新的Web3开发者关系公司。PaperMoon团队现诚聘一名中国开发者关系工程师&#xff0c;以协助支持Moonbeam上项目和开发者的部署。Moonbeam是一个智能合约平台&#xff0c;用于构建跨链互连应用程序&#xff0c;能够访问任何链上的用户、资产和服务。 我们正在…

删除的微信聊天记录如何恢复

微信成为了我们日常生活中必不可少的聊天工具&#xff0c;我们通过微信可以与家人、朋友、同事等人交流。由于工作和私人方面的对话往往会消耗大量的时间和空间&#xff0c;我们有时候也需要删除其中的一些聊天记录以释放空间。但是&#xff0c;一旦我们不小心将重要的聊天记录…

【Python】判断语句 ① ( if 语句 | if 语句语法 | 代码示例 )

文章目录 一、if 语句语法二、代码示例1、代码示例 - 触发 if 语句2、代码示例 - 不触发 if 语句 一、if 语句语法 在 Python 中 , 使用 if 语句进行判断 , 语法格式如下 : if 判断条件,布尔类型变量或表达式:条件成立,布尔类型变量或表达式为 True 执行的代码判断条件没有括号…

Three.js教程:点、线、网格模型介绍

推荐&#xff1a;将 NSDT场景编辑器 加入你的3D工具链 其他系列工具&#xff1a; NSDT简石数字孪生 点、线、网格模型介绍 经过前面几章学习相信你对点模型Points、线模型Line、网格模型Mesh已经有了大致了解&#xff0c;本节课就对点、线、网格模型模型进行简单总结。 点模型…

代数运算与代数系统

符号 I \mathbb{I} I表示整数 I \mathbb{I}_ I​表示正整数 ρ ( A ) \rho\left(A\right) ρ(A)&#xff1a;设 A A A是集合&#xff0c;则称 { x ∣ x ⊆ A } \left\{x|x\subseteq A\right\} {x∣x⊆A}为 A A A的幂集 代数运算 设 A A A为非空集合&#xff0c; n ∈ I n…

09-jQuery-动画-显示和隐藏元素

一、默认显示和隐藏方式 1、show()方法&#xff1a;可以显示被选元素&#xff0c;并且可以使用动画效果来实现平滑的过渡。它可以接受多个参数&#xff0c;其中第一个参数是动画的时长&#xff0c;第二个参数是动画的缓动类型&#xff0c;第三个参数是动画完成后的回调函数。 …

MAC安装Mysql

1.首先&#xff0c;登陆Mysql的 官网&#xff1a; https://www.mysql.com/downloads/ 2、进入官网后&#xff0c;页面拉到最下面&#xff0c;找到 Mysql Community&#xff08;GPL&#xff09; Downloads&#xff0c;点击进入下载 3.选择&#xff1a;Mysql Community Server…

react学习2

props基本用法&#xff0c;把属性自动保存到props里 简写&#xff1a;三点展开&#xff0c;展开运算符无法展开对象&#xff0c;但是三点外侧包裹花括号可以复制对象{...P} 对props的属性进行限制 首先需要引入prop-types.js包 之后再去进行限制 props是只读的&#xff0c;只…