Home > Article > CMS Tutorial > How to develop WordPress themes
Although free WordPress themes are everywhere now, as a Geek, have you ever considered writing your own WordPress theme? This article teaches you how to create a WordPress theme from the very basics.
Theme file structure:
Before writing, you must first understand the structure of the WordPress theme.
The WordPress theme is placed under wp-content/themes/ and exists as an independent folder. The name of the folder is arbitrary, but do not use pure numbers, otherwise the theme will not be displayed properly in the theme list. The theme folder contains all style files, template files, function files, JavaScript script files, static files, etc. required by the theme.
A minimal theme usually consists of three types of files:
Style sheet file style.css
Function file functions.php (optional)
Template files
Note that the names of these files are fixed and cannot be changed at will.
Let’s take a look at the function of each file separately.
Style file:
style.css is a required file for a theme because it contains descriptive information for the theme. The header information of a style.css is as follows:
/* Theme Name: 主题名称(必选) Theme URI: 主题的地址,可选,格式为一个URL,如http://wordpress.org/ Description: 对主题的描述,会显示在主题列表中。 Author: 作者 Version: 版本,如1.0 Tags: 给主题加的一些标签,可选,一般是为了让用户更方便搜索到这个主题。 */
It should be noted that each theme should have its own theme name (Theme Name) so that it can be distinguished in the theme list.
Function file:
Unless you create a purely static theme, you will definitely call the WordPress API. The functions used by these themes are written in the functions.php file. You can use the functions file in the WordPress theme as a reference.
Template file:
The template file is not a file, but a type of php file. They determine the final display of each of your pages. Template files follow certain naming rules. The following is the name and purpose of each template.
Template file description:
index.php
Main template. If your theme uses its own templates, index.php is required.
comments.php
Comment template.
front-page.php
Home page template, only used when the static home page is turned on.
home.php
Home page template, the default home page. If you enable the static homepage, this is a template page that displays the latest articles.
single.php
Single page template. Called when displaying a single article. For this and other request templates, index.php will be used if the template does not exist.
single-.php
Customize a single page template. For example, single-books.php displays articles with a custom article type of books. If the article type is not set, index.php is used.
page.php
Page template, independent page call.
category.php
Category template, category page call.
tag.php
Tag template, tag page call.
taxonomy.php
Term template, used when requesting terms for a custom taxonomy.
author.php
Author template, called by the author page.
date.php
Date/time template, the template used when querying by time.
archive.php
Archive template, the template used when querying categories, authors or dates. It should be noted that this template will be overwritten by category.php, author.php, and date.php respectively (if they exist).
search.php
Search result template, the template used when displaying search results.
attachment.php
Attachment template, the template used when viewing a single attachment.
image.php
Image attachment template, this template will be called when viewing a single image in wordpress. If this template does not exist, the attachment.php template will be called.
404.php
404 error page template, used when WordPress cannot find a log or page that matches the query, the 404.php file is used.
There is no special quantity requirement. You can even use just one file index.php as a template file. All pages will use this template. In most cases, you will have multiple templates to display different pages. The calling sequence of the specific template can refer to the figure below:
The simplest theme:
Know the above After that, let’s look at an example of the simplest theme. First of all, this theme contains the following files:
style.css
index.php
single.php
header.php
sidebar.php
footer.php
The content of style.css has been mentioned above. You only need to add the css you need.
Header.php, sidebar.php, footer.php categories are the top, sidebar, and tail of the page. Articles use single.php as the template, and other pages (such as the homepage) will use index.php as the template. The content of
index.php is:
<?php get_header(); ?> <?php get_sidebar(); ?> <?php get_footer(); ?>
The article page template single.php is:
<?php get_header(); ?> <h1><?php the_title(); ?></h1> <div><?php the_content(); ?></div> <?php get_sidebar(); ?> <?php get_footer(); ?>
这样,我们一个最简单的模板就完成了。剩下的就是你自己根据你的需要为其添加样式和内容了。另外在模板中所有你可能用到的Wordpress函数在这里都可以找到:Wordpress Function Reference。
更多wordpress相关技术文章,请访问wordpress教程栏目进行学习!
The above is the detailed content of How to develop WordPress themes. For more information, please follow other related articles on the PHP Chinese website!