I introduced you to "The whole process of WordPress theme production (4): A small test". This article continues to bring you "The whole process of WordPress theme production (5): Making header.php" , let’s take a look at it together~
You can try to use a text editor to open the.html downloaded fromWordPress theme production process (3): HTML static template productionfiles, I wonder if you have noticed that the codes in their headers are very similar? In fact, we can extract this part of similar code and put it into a separate file
header.php
. When each page wants to use this part of the code, use php'sinclude()
or WordPress'sget_header()
is included, and this part of the code must be written in every page in the province. If you change it, you can achieve the purpose of making a complete change.
Remind me again:If you don’t plan to write code, don’t read this series of tutorials, it will not be helpful to you!
Then the theme directory we created last timewp-content\themes\Aurelius
, create a new php fileheader.php
in this directory, we extract# Copy and paste the header code in ##index.phpinto
header.php. The following code is all the code currently in
header.php(different themes of course The header codes are all different and can be customized in your actual project):
Aurelius | Blog Then use a text editor to openAurelius
Our blog, keeping you up-to-date on our latest news.
Copy after loginindex.php
,
archive.php,
contact.php,
full_width.php,
page.phpand
single.php, Delete the above similar code and change it to:
Okay, now open your test blog homepage to see if the theme we made can still work normally. The answer is yes, follow It turned out to be almost the same, but still chaos.Copy after loginget_header()
is equivalent to copying the code in
header.phpto the current php file. Next, we'll take a closer look at the dynamic content in
header.php.
header.phpwill be included in all template pages (home page, category page, page, tag page, etc.), so the code in
header.phpshould be dynamic and suitable for different pages , so PHP code needs to be used here, not simple HTML. Let's modify
header.php:
1. Change
We all know that the titles of different pages are It’s different, and the title setting will also directly affect the SEO effect, so you should set it carefully here. The following provides an SEO-optimized title writing method. ChangeAurelius | Blog to:
The PHP code added above uses conditional judgment to target different The pages use different titles. Here are some conditional tags explained.Copy after login
: Returns true when the current page is the homepage
: Returns true when the current page is a category page
: Returns true when the current page is a single article page
: Returns true when the current page is a single page
WordPress SEO title
header.php:
wp-content\themes\AureliusIsn’t there already a
style.cssin the directory? So why is
header.phpnot loading css? As you can see the result, the page is in a mess, and you can be sure that the css is not loaded. Because this is a theme of WordPress, it needs to be called by the main program of WordPress, and your blog can be displayed after layers of analysis, rather than a simple html static web page file. Correct modification:
bloginfo('stylesheet_url')
输出的是你的主题css文件绝对网址,如http://localhost/wp/wp-content/themes/Aurelius/style.css,WordPress程序会自动识别你的WordPress安装地址,当前启用的主题,自动输出这个style.css链接。现在你可以试着更改一下,然后刷新一下你的博客首页,查看网页源代码,style.css的链接是不是变成你的了?页面是否可以正常显示了呢?
如果你的css文件不是style.css,且不是在主题根目录下,那怎么办呢?我们可以用来获取主题根目录的URL,如你的主题css文件是
abc.css
,那么我们可以这样写:/abc.css
,如果是在子目录css下那就这样:/css/abc.css
。同样加载js文件也是这样。
不过,还有几张图片的路径不对,还不能显示出来,现在我们一起用文本编辑器打开index.php
、archive.php
、contact.php
、full_width.php
、page.php
和single.php
,给这些图片加上正确的URL,搜索代码,将所有的:src="images/
,批量替换成src="/images/
。现在再刷新你的主页,看文章的缩略图是否可以正常显示。用于输出主题目录的URL。
至于什么是pingback,你可以在搜索引擎中输入关键字:WordPress pingback
,就可以得到你想要的答案了。如果你需要这个功能,可以在里面添加以下代码:
在header.php
,下面两行代码用于显示博客名称和描述:
Aurelius
Our blog, keeping you up-to-date on our latest news.
上面是静态代码,现在做如下修改:
现在你的博客首页看到的就是你博客名称和描述了,并且logo也是一个链接指向你的博客首页。我们这里说说这些php代码的作用。
输出你的博客首页网址
输出你的博客名称
输出博客描述博客名称和描述可以在WordPress管理后台 - 设置 - 常规那里更改。以后制作你自己的WordPress主题的时候,你可参照上面的说明对你的主题进行修改。
相信每个已发布的WordPress博客主题都会提供feed订阅,当然我们的主题也应该提供这样的功能。在
之前添加以下代码:
有些插件需要在网页头部执行一些类如添加一些js或css的动作,要让这些插件能够正常的工作,也让你的主题有更好的兼容性,你应该添加wp_head()函数。打开header.php
,在
前面添加以下代码即可:
现在打开你的博客主页,查看源代码,
前面是不是多了以下类似代码(这些都是wp_head()
的功劳):
关于添加网页描述和关键字,可以查看我之前写过的文章:WordPress使用经验(一)独立的Description 和 Keywords
目前菜单栏有Home、Blog和Contact Us几个菜单,不过这些都是静态的内容,并不是你博客上的页面。现在我们将菜单栏换成你的菜单,这里只在菜单栏中列出页面page,当然你也可以再放置分类,根据你的喜好来吧,将header.php中:
改成:
The following two articles introduce how to make WordPress menus, you can also refer to:
Add PHP code in front ofand after
to improve program running efficiency:
Okay, this exercise is over! Now summarize some of the more important knowledge points mentioned today:
Include the header.php file from the current theme folder
and several other conditional judgment tags
Output the path to the style.css file in the theme folder
Output the blog pingback URL
Output the blog theme directory URL
Output your blog name
Output blog description
Used to include the WordPress program output header Department information
Used to list blog category pages
Used to list blog pages
Aureliustheme file after this modification is provided. You can open it with a text editor and compare it with the file you modified (especially
header.php). See How are you doing?
WordPress Tutorial"
The above is the detailed content of The whole process of WordPress theme creation (5): making header.php. For more information, please follow other related articles on the PHP Chinese website!