The whole process of WordPress theme creation (5): making header.php
Feb 21, 2023 am 10:21 AMI 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
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 to:
<title><?php if ( is_home() ) { bloginfo('name'); echo " - "; bloginfo('description'); } elseif ( is_category() ) { single_cat_title(); echo " - "; bloginfo('name'); } elseif (is_single() || is_page() ) { single_post_title(); } elseif (is_search() ) { echo "搜索結(jié)果"; echo " - "; bloginfo('name'); } elseif (is_404() ) { echo '頁面未找到!'; } else { wp_title('',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
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 inheader.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('stylesheet_url'); ?>" type="text/css" media="screen" />
bloginfo('stylesheet_url')
輸出的是你的主題css文件絕對網(wǎng)址,如http://localhost/wp/wp-content/themes/Aurelius/style.css,WordPress程序會自動識別你的WordPress安裝地址,當前啟用的主題,自動輸出這個style.css鏈接?,F(xiàn)在你可以試著更改一下,然后刷新一下你的博客首頁,查看網(wǎng)頁源代碼,style.css的鏈接是不是變成你的了?頁面是否可以正常顯示了呢?
如果你的css文件不是style.css,且不是在主題根目錄下,那怎么辦呢?我們可以用<?php bloginfo('template_url'); ?>
來獲取主題根目錄的URL,如你的主題css文件是abc.css
,那么我們可以這樣寫:<?php bloginfo('template_url'); ?>/abc.css
,如果是在子目錄css下那就這樣:<?php bloginfo('template_url'); ?>/css/abc.css
。同樣加載js文件也是這樣。
不過,還有幾張圖片的路徑不對,還不能顯示出來,現(xiàn)在我們一起用文本編輯器打開index.php
、archive.php
、contact.php
、full_width.php
、page.php
和single.php
,給這些圖片加上正確的URL,搜索代碼,將所有的:src="images/
,批量替換成src="<?php bloginfo('template_url'); ?>/images/
?,F(xiàn)在再刷新你的主頁,看文章的縮略圖是否可以正常顯示。<?php bloginfo('template_url'); ?>
用于輸出主題目錄的URL。
3、添加pingback
至于什么是pingback,你可以在搜索引擎中輸入關(guān)鍵字:WordPress pingback
,就可以得到你想要的答案了。如果你需要這個功能,可以在<head>
里面添加以下代碼:
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
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>
上面是靜態(tài)代碼,現(xiàn)在做如下修改:
<h1 id="logo" class="grid_4"><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1> <h2 class="grid_12 caption clearfix"><?php bloginfo('description'); ?></h2>
現(xiàn)在你的博客首頁看到的就是你博客名稱和描述了,并且logo也是一個鏈接指向你的博客首頁。我們這里說說這些php代碼的作用。
<?php echo get_option('home'); ?>
輸出你的博客首頁網(wǎng)址<?php bloginfo('name'); ?>
輸出你的博客名稱<?php bloginfo('description'); ?>
輸出博客描述
博客名稱和描述可以在WordPress管理后臺 - 設(shè)置 - 常規(guī)那里更改。以后制作你自己的WordPress主題的時候,你可參照上面的說明對你的主題進行修改。
5、添加訂閱feed鏈接
相信每個已發(fā)布的WordPress博客主題都會提供feed訂閱,當然我們的主題也應(yīng)該提供這樣的功能。在</head>
之前添加以下代碼:
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有文章" href="<?php echo get_bloginfo('rss2_url'); ?>" /> <link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有評論" href="<?php bloginfo('comments_rss2_url'); ?>" />
6、添加wp_head
有些插件需要在網(wǎng)頁頭部執(zhí)行一些類如添加一些js或css的動作,要讓這些插件能夠正常的工作,也讓你的主題有更好的兼容性,你應(yīng)該添加wp_head()函數(shù)。打開header.php
,在</head>
前面添加以下代碼即可:
<?php wp_head(); ?>
現(xiàn)在打開你的博客主頁,查看源代碼,</head>
前面是不是多了以下類似代碼(這些都是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='index' href='http://ludou.co.tv' /> <meta name="generator" content="WordPress 2.9.2" />
7、添加Description 和 Keywords
關(guān)于添加網(wǎng)頁描述和關(guān)鍵字,可以查看我之前寫過的文章:WordPress使用經(jīng)驗(一)獨立的Description 和 Keywords
8、顯示菜單欄
目前菜單欄有Home、Blog和Contact Us幾個菜單,不過這些都是靜態(tài)的內(nèi)容,并不是你博客上的頁面?,F(xiàn)在我們將菜單欄換成你的菜單,這里只在菜單欄中列出頁面page,當然你也可以再放置分類,根據(jù)你的喜好來吧,將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('depth=1&title_li=0&sort_column=menu_order'); ?> <li <?php if (is_home()) { echo 'class="current"';} ?>><a title="<?php bloginfo('name'); ?>" href="<?php echo get_option('home'); ?>/">主頁</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 <body>
and after </head>
to improve program running efficiency: <?php flush(); ?>
Summary
Okay, this exercise is over! Now summarize some of the more important knowledge points mentioned today:
- ##<?php get_header(); ?>
Include the header.php file from the current theme folder
- is_home(), is_single(), is_category()
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 homepage URL
Output your blog name
-
Output blog description
- <?php wp_head(); ?>
Used to include the WordPress program output header Department information
Used to list blog category pages
Used to list blog pages
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?
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
