While the most minimal of WordPress Themes really only need an index.php template and a style.css file (or just the style file if it’s a Child Theme) most need something a little more solid.

Let’s create the directories and files that will make up our _s-based theme, Shape. Make a folder in wp-content/themes/ for your theme—for this tutorial I’ll be using “shape”, but it can be whatever you want—and create the following blank files and folders in that new folder (don’t worry, we’ll fill them up as we work through the lessons).

  • inc (folder)
  • js (folder)
  • languages (folder)
  • layouts (folder)
  • 404.php
  • archive.php
  • comments.php
  • content.php
  • content-aside.php
  • content-page.php
  • content-single.php
  • footer.php
  • functions.php
  • header.php
  • index.php
  • no-results.php
  • page.php
  • search.php
  • searchform.php
  • sidebar.php
  • single.php
  • license.txt
  • rtl.css
  • style.css

Now let’s open up the last file we created, style.css, in a text editor. The first thing we need to do is add a section at the top of this file bracketed by what are called CSS “comments” (these guys: /* and */). It’s here that we need to put the info that tells WordPress about your theme. Without it, your theme won’t show up in the themes panel.

I’m using “Shape” as the theme name here, but feel free to name your theme what you want. And of course, fill in the author name, URLs, and description with your own information. 

01
02
03
04
05
06
07
08
09
10
11
12
13
/*
Theme Name: Shape
Theme URI: http://themeshaper.com/
Author: ThemeShaper
Author URI: http://themeshaper.com/
Description: The Shape theme is a simple, minimalist theme based on Underscores and the original Shape Theme by Ian Stewart. It was created especially as a learning theme for The ThemeShaper WordPress Theme Tutorial: 2nd Edition.
Version: 2.0
License: GNU General Public License
License URI: license.txt
Tags: light, white, one-column, two-columns, left-sidebar, right-sidebar, flexible-width, custom-backgroud, custom-header, custom-menu, featured-images, flexible-header, microformats, post-formats, rtl-language-support, threaded-comments, translation-ready
This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/

Let’s walk through each of these elements so you understand what they’re all about.

  • Theme Name – The name of your theme, obviously!
  • Theme URL – The URL of your theme’s home on the web. It can be a section of your website. For example, many theme authors will use something like: “http://yourgroovydomain.com/your-theme/”
  • Author – Self-explanatory. Your name, of course!
  • Author URI – Link to your website
  • Description – Provide a brief and clear description of your theme, summarizing its purpose and features in a few sentences. This description will appear in users’ Dashboards when they search for themes, as well as next to the theme’s listing on the WordPress.org Free Themes Directory.
  • Version – The version number of your theme. It’s up to you to decide how to number your versions, but you can start at 1.0 if it’s brand new. If you ever release updates, you can change the number accordingly.
  • License – Your theme’s license. If you’re distributing your theme, it’s required to use the GPL license, the license that WordPress uses.
  • License URI – Provide a link to where users can find the text of the license. We’re including a “license.txt” file with the theme that we’ll fill in in our lesson on Distributing Your WordPress Theme.
  • Tags – These words describe your theme’s features, colors, and subjects. They are required if you plan to distribute your theme. These tags allow people to filter their searches by color, subject, etc., when searching for themes in the WordPress Free Themes Directory or in their Dashboards. Here’s a list of approved tags.

Something to note: a lot of this is optional. Really, you just need the Theme Name. But if you ever plan on releasing your theme, or if you’re making a custom theme for someone, you’ll want to start out including most, if not all, of the rest. At the very least, I want you to feel free to mess around with it.

Once you’ve got that done you can activate your theme and navigate to your test site. We’ve made the ultimate blank theme! Things should start to get interesting right about now.

Building In Your HTML Structure

Now we get to use our HTML structure from the previous lesson. But first, a mini-lesson about WordPress and Templates.

WordPress really only needs 1 template file, index.php. We can, and will be using a series of template files that can be used instead of index.php for certain situations (single posts, archive pages, etc.), but at the very beginning, index.php is all we’ll need.

Now, index.php and all its related brothers and sisters (which we’ll get to) make the web pages we see in our browser. They’re files with some HTML and HTML-outputting-PHP but in the end they make web pages.

Let’s think of web pages like stories, something with a beginning, a middle, and an end. Well, when we write out our index.php file (and later our single.php, category.php, etc.) we’re going to concentrate only on the middle bit. But! We’re going to call in the beginning bit and the end bit. We may have to always be redoing our middles but we’re only going to do the beginning and end of each web page once.

Header.php and Footer.php

Get the HTML structure we worked on in the previous lesson and copy everything up to and including <div id=”main”> into header.php and save it. It should look like this:

1
2
3
4
5
6
<div id="page" class="hfeed site">
     <header id="masthead" class="site-header" role="banner">
         <hgroup></hgroup>
         <nav role="navigation" class="site-navigation main-navigation"></nav><!-- .site-navigation .main-navigation -->
     </header><!-- #masthead .site-header -->
<div id="main" class="site-main">

Now, copy everything after, and including, </div><!– #main –> into footer.php. It should look like this:

1
2
3
4
5
     </div><!-- #main .site-main -->
     <footer id="colophon" class="site-footer" role="contentinfo">
          <div class="site-info"></div><!-- .site-info -->
     </footer><!-- #colophon .site-footer -->
</div><!-- #page .hfeed .site -->

Sidebar.php

Copy the following from our HTML structure into sidebar.php. It should look like this:

<div id="secondary" class="widget-area">
</div><!-- #secondary .widget-area --> <div id="tertiary" class="widget-area">
</div><!-- #tertiary .widget-area -->

Index.php

I bet you can guess what we have to do now. Copy everything from our HTML structure inside the #main div up until the #primary closing div into index.php. It should look like this:

<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
</div><!-- #content .site-content -->
</div><!-- #primary .content-area -->

With only two small additions we’ll have a perfectly invalid WordPress Theme but we’ll be on the right track. We need to call in the header, sidebars, and footer to your theme.

At the top of index.php, before anything else, add the following template tag.

1
<?php get_header(); ?>

All right! Can you guess which function calls we’re going to put at the bottom of index.php?

1
2
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Yep. Now we’ve got our main file that WordPress looks for, index.php. It has all the middle bits of our web page, but the top calls in the beginning bits, and the bottom calls in the ending bits.

Reload your page in the browser and check out the source code (View > Page Source, in Firefox). Look! It’s your code!

You’re on your way to making your first WordPress Theme.

WordPress 主题开发 - (五)WordPress 主题模板及目录结构 待翻译的更多相关文章

  1. Android开发系列之Android项目的目录结构

    今天开始正式学习Android开发的种种细节,首先从最基本的概念和操作学起. 首先看一下Android项目的目录结构. 这是我随便建立的一个test项目,我们重点关注一下几个方面的内容: 1.src目 ...

  2. iOS开发——高级篇——iOS 项目的目录结构

    最近闲来无事去面试一下iOS开发,让我感到吃惊的,面试官竟然问怎么分目录结构,还具体问每个子目录的文件名. 目录结构确实非常重要,面试官这么问,无疑是想窥探开发经验.清晰的目录结构,可让人一眼明白相应 ...

  3. 第五篇 scrapy安装及目录结构,启动spider项目

    实际上安装scrapy框架时,需要安装很多依赖包,因此建议用pip安装,这里我就直接使用pycharm的安装功能直接搜索scrapy安装好了. 然后进入虚拟环境创建一个scrapy工程: (third ...

  4. WordPress 主题开发 - (十二) Search模板与Page模板 待翻译

    The Search Template and The Page Template are vital to any complete WordPress Theme. And they're bot ...

  5. WordPress 主题开发 - (二) 理解主题 待翻译

    What is “Theme Sense”? What is “Theme Sense”? Theme Sense is an intuitive understanding of WordPress ...

  6. WordPress 主题开发 - (七) 让主题更安全 待翻译

    We're just about ready to start building our theme's template files. Before we do this, however, it' ...

  7. WordPress 主题开发 - (六) 创建主题函数 待翻译

    We’ve got a file structure in place, now let’s start adding things to them! First, we’re going to ad ...

  8. Cocoa Touch(一)开发基础:Xcode概念、目录结构、设计模式、代码风格

    Xcode相关概念: 概念:project 指一个项目,该项目会负责管理软件产品的全部源代码文件.全部资源文件.相关配置,一个Project可以包含多个Target. 概念:target 一个targ ...

  9. android studio 开发免安装的app之目录结构

    尚未深入分析,暂且外链到我看到的,对此有帮助的博客,在此,感谢你们. 1.https://blog.csdn.net/tscyds/article/details/74331085 2.https:/ ...

随机推荐

  1. 跨平台网络库(采用C++ 11)

    I:跨平台设计基础 在windows下使用0字节的WSARecv/WSASend(读写)作为读写检测,将IOCP作为一个通知模型,而"抛弃"它的异步模型. 即:把它当作epoll来 ...

  2. 【开源项目8】Android开源项目分类汇总【畜生级别】

    欢迎大家推荐好的Android开源项目,可直接Commit或在 收集&提交页 中告诉我,欢迎Star.Fork :) 微博:Trinea    主页:www.trinea.cn    邮箱:t ...

  3. Android中Handler作用

    在Android的UI开发中,我们经常会使用Handler来控制主UI程序的界面变化.有关Handler的作用,我们总结为:与其他线程协同工作,接收其他线程的消息并通过接收到的消息更新主UI线程的内容 ...

  4. 重构20-Extract Subclass(提取父类)

    当一个类中的某些方法并不是面向所有的类时,可以使用该重构将其迁移到子类中.我这里举的例子十分简单,它包含一个Registration类,该类处理与学生注册课程相关的所有信息. public class ...

  5. 关于automatic_Panoramic_Image_Stitching_using_Invariant_features 的阅读笔记(2)

    接上一篇: http://www.cnblogs.com/letben/p/5446074.html#3538201 捆绑调整 (好开心有同学一起来看看这些问题,要不然就是我自己的话,我应该也不会看的 ...

  6. asp.net mssqlserver 存储过程

    mssql server 返回多表结果集 mssqlserver 代码 create PROCEDURE [dbo].[gd] AS BEGIN , , END C#代码 using (SqlConn ...

  7. javascript线程解释(setTimeout,setInterval你不知道的事)---转载

    在工作中,可能我们经常遇到在有很多 setInterval 的页面, 再手动触发 setTimeout 的时候经常失败, 尤其是 jquery做动画的时候,一些渐入溅出的东西,很多东西都不被触发……, ...

  8. 转: app端数据库(性能高) realm (ios, android 均支持)

    转:  http://ios.jobbole.com/85041/ 移动端数据库新王者:realm 2016/05/14 · iOS开发 · 数据库 分享到:0 原文出处: 没故事的卓同学(@没故事的 ...

  9. Application Loader上传app时报错:the bundle identifier cannot be changed from the current value

    报错如图: 解决:用info.plist中的bundle identifier生成发布证书(Distribution),如图:

  10. Java作业 输入圆的半径计算输出圆的周长和面积

    package text1; import java.util.Scanner; public class text11 { public static void main(String[] args ...