Home >CMS Tutorial >WordPress >The whole process of WordPress theme creation (5): making header.php

The whole process of WordPress theme creation (5): making header.php

青灯夜游
青灯夜游forward
2023-02-21 10:21:471934browse

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 from WordPress theme production process (3): HTML static template production files, 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's include() or WordPress's get_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 file header.php in this directory, we extract# Copy and paste the header code in ##index.php into 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):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aurelius | Blog</title>
<!-- Stylesheets -->
<link rel="stylesheet" href="./style.css" type="text/css" media="screen" />
</head>
<body>
<div id="wrapper" class="container_12 clearfix">
	<!-- Text Logo -->
	<h1 id="logo" class="grid_4">Aurelius</h1>
	<!-- Navigation Menu -->
	<ul id="navigation" class="grid_8">
		<li><a href="contact.html"><span class="meta">Get in touch</span><br />
			Contact Us</a></li>
		<li><a href="blog.html" class="current"><span class="meta">Latest news</span><br />
			Blog</a></li>
		<li><a href="index.html"><span class="meta">Homepage</span><br />
			Home</a></li>
	</ul>
	<div class="hr grid_12 clearfix"> </div>
	<!-- Caption Line -->
	<h2 class="grid_12 caption clearfix">Our <span>blog</span>, keeping you up-to-date on our latest news.</h2>
	<div class="hr grid_12 clearfix"> </div>
Then use a text editor to open

index.php, archive.php, contact.php, full_width.php, page.php and single.php, Delete the above similar code and change it to:

<?php get_header(); ?>
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.

get_header() is equivalent to copying the code in header.php to the current php file. Next, we'll take a closer look at the dynamic content in header.php. header.php will be included in all template pages (home page, category page, page, tag page, etc.), so the code in header.php should 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 b2386ffb911b14667cb8f0f91ea547a7

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. Change

b2386ffb911b14667cb8f0f91ea547a7Aurelius | Blog6e916e0f7d1e588d4f442bf645aedb2f to:

<title><?php if ( is_home() ) {
		bloginfo(&#39;name&#39;); echo " - "; bloginfo(&#39;description&#39;);
	} elseif ( is_category() ) {
		single_cat_title(); echo " - "; bloginfo(&#39;name&#39;);
	} elseif (is_single() || is_page() ) {
		single_post_title();
	} elseif (is_search() ) {
		echo "搜索结果"; echo " - "; bloginfo(&#39;name&#39;);
	} elseif (is_404() ) {
		echo &#39;页面未找到!&#39;;
	} else {
		wp_title(&#39;&#39;,true);
	} ?></title>

The PHP code added above uses conditional judgment to target different The pages use different titles. Here are some conditional tags explained.

  • is_home(): Returns true when the current page is the homepage
  • is_category(): Returns true when the current page is a category page
  • is_single(): Returns true when the current page is a single article page
  • is_page(): Returns true when the current page is a single page
So far, maybe you don’t have a deep understanding of these conditional judgment tags, and you don’t understand how using these tags will affect the theme. As our tutorial progresses, Go deeper and you will slowly understand. If you don’t like the way the title is written above, you can search for relevant codes online:

WordPress SEO title

2. Change the style.css path of the style sheet

Before this, the home page you saw was in chaos because the css style has not been loaded. Now let's add the styles together. You can find this piece of code in

header.php:

<link rel="stylesheet" href="./style.css" type="text/css" media="screen" />
If you are smart, you may ask:

wp-content\themes\Aurelius Isn’t there already a style.css in the directory? So why is header.php not 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:

<link rel="stylesheet" href="<?php bloginfo(&#39;stylesheet_url&#39;); ?>" type="text/css" media="screen" />

bloginfo('stylesheet_url')输出的是你的主题css文件绝对网址,如http://localhost/wp/wp-content/themes/Aurelius/style.css,WordPress程序会自动识别你的WordPress安装地址,当前启用的主题,自动输出这个style.css链接。现在你可以试着更改一下,然后刷新一下你的博客首页,查看网页源代码,style.css的链接是不是变成你的了?页面是否可以正常显示了呢?

如果你的css文件不是style.css,且不是在主题根目录下,那怎么办呢?我们可以用0fc0a3f621cc574c92fe12d951515ac9来获取主题根目录的URL,如你的主题css文件是abc.css,那么我们可以这样写:0fc0a3f621cc574c92fe12d951515ac9/abc.css,如果是在子目录css下那就这样:0fc0a3f621cc574c92fe12d951515ac9/css/abc.css。同样加载js文件也是这样。

不过,还有几张图片的路径不对,还不能显示出来,现在我们一起用文本编辑器打开index.phparchive.phpcontact.phpfull_width.phppage.phpsingle.php,给这些图片加上正确的URL,搜索代码,将所有的:src="images/,批量替换成src="0fc0a3f621cc574c92fe12d951515ac9/images/。现在再刷新你的主页,看文章的缩略图是否可以正常显示。0fc0a3f621cc574c92fe12d951515ac9用于输出主题目录的URL。

3、添加pingback

至于什么是pingback,你可以在搜索引擎中输入关键字:WordPress pingback,就可以得到你想要的答案了。如果你需要这个功能,可以在93f0f5c25f18dab9d176bd4f6de5d30e里面添加以下代码:

<link rel="pingback" href="<?php bloginfo(&#39;pingback_url&#39;); ?>" />

4、更改博客名称和描述

header.php,下面两行代码用于显示博客名称和描述:

<h1 id="logo" class="grid_4">Aurelius</h1>
<h2 class="grid_12 caption clearfix">Our <span>blog</span>, keeping you up-to-date on our latest news.</h2>

上面是静态代码,现在做如下修改:

<h1 id="logo" class="grid_4"><a href="<?php echo get_option(&#39;home&#39;); ?>/"><?php bloginfo(&#39;name&#39;); ?></a></h1>
<h2 class="grid_12 caption clearfix"><?php bloginfo(&#39;description&#39;); ?></h2>

现在你的博客首页看到的就是你博客名称和描述了,并且logo也是一个链接指向你的博客首页。我们这里说说这些php代码的作用。

  • 8e15115cb80d1acbe35508806bdafbee  输出你的博客首页网址
  • dbc395af03a8e84af3b585d66d8154e4  输出你的博客名称
  • 512e8410679877c13c3741899e256d1d  输出博客描述

博客名称和描述可以在WordPress管理后台 - 设置 - 常规那里更改。以后制作你自己的WordPress主题的时候,你可参照上面的说明对你的主题进行修改。

5、添加订阅feed链接

相信每个已发布的WordPress博客主题都会提供feed订阅,当然我们的主题也应该提供这样的功能。在9c3bca370b5104690d9ef395f2c5f8d1之前添加以下代码:

<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有文章" href="<?php echo get_bloginfo(&#39;rss2_url&#39;); ?>" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有评论" href="<?php bloginfo(&#39;comments_rss2_url&#39;); ?>" />

6、添加wp_head

有些插件需要在网页头部执行一些类如添加一些js或css的动作,要让这些插件能够正常的工作,也让你的主题有更好的兼容性,你应该添加wp_head()函数。打开header.php,在9c3bca370b5104690d9ef395f2c5f8d1前面添加以下代码即可:

<?php wp_head(); ?>

现在打开你的博客主页,查看源代码,9c3bca370b5104690d9ef395f2c5f8d1前面是不是多了以下类似代码(这些都是wp_head()的功劳):

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://ludou.co.tv/blog/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://ludou.co.tv/blog/wp-includes/wlwmanifest.xml" /> 
<link rel=&#39;index&#39;  href=&#39;http://ludou.co.tv&#39; />
<meta name="generator" content="WordPress 2.9.2" />

7、添加Description 和 Keywords

关于添加网页描述和关键字,可以查看我之前写过的文章:WordPress使用经验(一)独立的Description 和 Keywords

8、显示菜单栏

目前菜单栏有Home、Blog和Contact Us几个菜单,不过这些都是静态的内容,并不是你博客上的页面。现在我们将菜单栏换成你的菜单,这里只在菜单栏中列出页面page,当然你也可以再放置分类,根据你的喜好来吧,将header.php中:

<ul id="navigation" class="grid_8">
	<li><a href="contact.html"><span class="meta">Get in touch</span><br />
		Contact Us</a></li>
	<li><a href="blog.html" class="current"><span class="meta">Latest news</span><br />
		Blog</a></li>
	<li><a href="index.html"><span class="meta">Homepage</span><br />
		Home</a></li>
</ul>

改成:

<ul id="navigation" class="grid_8">
	<?php wp_list_pages(&#39;depth=1&title_li=0&sort_column=menu_order&#39;); ?>
	<li <?php if (is_home()) { echo &#39;class="current"&#39;;} ?>><a title="<?php bloginfo(&#39;name&#39;); ?>"  href="<?php echo get_option(&#39;home&#39;); ?>/">主页</a></li>
</ul>

The following two articles introduce how to make WordPress menus, you can also refer to:

9. Refresh the cache

Add PHP code in front of 6c04bd5ca3fcae76e30b72ad730ca86d and after 9c3bca370b5104690d9ef395f2c5f8d1 to improve program running efficiency: aac5cfcd9c80ae0a9200fcac4073e209

Summary

Okay, this exercise is over! Now summarize some of the more important knowledge points mentioned today:

  • ##c974613bee707296608324a2f9bc69b1 Include the header.php file from the current theme folder
  • is_home(), is_single(), is_category() and several other conditional judgment tags
  • 32b7ffa9d311bb22362c26f0872cc034 Output the path to the style.css file in the theme folder
  • 65eaf82458e40adb7554a73961907d8f Output the blog pingback URL
  • 373480fef4095982fa440ea01a9f995c Output the blog theme directory URL
  • f3b4d0753ca5f5141ec1a43556f0880c Output your blog name
  • aba501e6506f9f6f8f201b13932b065a Output blog description
  • 12493c822e68bbb57c539d2976ef876e Used to include the WordPress program output header Department information
  • 57e4661e8da6ea5673b0f98014eea780 Used to list blog category pages
  • d5efcc22cc7ace15478d5c20b21eee5d Used to list blog pages
So far, you can only see the homepage of your blog. Don’t be discouraged. Take everything one step at a time. The tutorials will be gradually in-depth in the future. Finally, the

Aurelius theme 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?

The whole process of WordPress theme creation (5): making header.php

Recommended study: "

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!

Statement:
This article is reproduced at:ludou.org. If there is any infringement, please contact admin@php.cn delete