Home > CMS Tutorial > WordPress > body text

How to make WordPress theme code static? Tutorial sharing

青灯夜游
Release: 2023-03-17 20:29:10
forward
1489 people have browsed it

How to make WordPress theme code static? The following article will share with you the tutorial on staticizing WordPress theme code. I hope it will be helpful to you!

How to make WordPress theme code static? Tutorial sharing

The so-called staticization of WordPress theme code is to replace the dynamic code in the WordPress theme with static content. Maybe you don’t know what dynamic code is, so think about it first. Question: Why does the same WordPress theme display the title "Pandou Blog" when used on my blog, but displays another title when used on your blog? The answer lies in the dynamic code in the theme, which will display different content according to different users, different usage environments, and even different times. But if this theme is only used on your blog, then many things will be fixed, such as the blog title. You no longer need the theme to dynamically display these static content, and dynamic code often consumes more time than static content.

Now that we know what dynamic code is, let me introduce how to make your theme code static. It should be noted that the static theme can only be used for your blog. If the domain name and other information are changed, the code will need to be modified again; before starting, you'd better prepare a text that can modify and search multiple files at the same time. An editor, such as UltraEdit, will be more convenient, because the same piece of code will appear in multiple files of the theme. It doesn't matter if you don't have such an editor, but it is best not to use the Notepad that comes with Windows to change the code; in addition, If you modify the code and there is Chinese in it, please save it in UTF-8, otherwise the Chinese will be garbled.

Finally, let me introduce the layout rules of this article. There will be some bold codes under each green main title below. These are dynamic PHP codes that need to be replaced. You can open all the codes in the theme folder. .php file, find these codes and replace them according to the instructions, such as provided in the first item, because the number of spaces and parameters used in different theme codes are different. It’s too similar, so you may not be able to find it by directly searching for the above code. You can just search for language_attributes. If you have any questions, you can leave me a message. Too much nonsense, sorry! Let’s start with the main topic:

1. Web page language attribute declaration

This function is used to declare The language used by the web page generally appears at the beginning of the header.php file. If your theme is for a Chinese site and the text direction is read from left to right, you can use the following static content instead of adding The thick piece of code: dir="ltr" lang="zh-CN"

The modified code is similar: <html dir="ltr" lang=" zh-CN">

2. bloginfo() blog information function

This function has many parameters (the content in brackets), pass Different parameters can be used to output different blog information. For the specific content of each parameter output, you can take a look at the document: template tag-bloginfo. Let’s pick up a few common ones:

is used to output the "site title" of the blog. As for the site title, you can Find it in the WordPress backend - Settings - General, replace all this code with your "site title"; ##The "subtitle" used to output the blog can be found in the WordPress backend - Settings - General. Replace all this code with your "subtitle";

is used to output the "site address (URL)" of the blog, which can be found in the WordPress backend - Settings - General. Replace all this code with your "site address (URL)" URL)";

Used to output the "style.css file URL" of the blog, if you don't know What is this URL? You can open your blog homepage, and then use the browser's "View Source Code" function to view the source code of the web page. Search for style.css. You should be able to find http://example/wp-content/themes/ default/style.css, just replace this code with this URL;

Used to output the "style "The directory where the .css file is located", that is, remove /style.css from the style.css URL above, such as http://example/wp-content/themes/default, remember there is no / after it;

    Used to output the URL of your feed, the general form is: http://example/feed/, if you use Feedsky The class hosts the feed, which can be replaced by the URL provided by Feedsky; The form is as follows: http://example/home/wp/xmlrpc.php, check the source code and search for rel="pingback", you can find this URL; '); ?>

This function is used to declare the encoding of the web page. It usually appears at the beginning of this file

header.php. This encoding can be found in the WordPress background - Settings - Reading, the last option "page and feed encoding" are set there, usually UTF-8. You can replace this code with: UTF-8

is used to output the version number of WordPress. However, due to security reasons, it is not recommended to use this function. You can replace this code with a non-existent version number, which can also be confusing to a certain extent. effect. 3. get_option() Blog information function

This function can output a lot of blog information like bloginfo(), but it will not print it out directly, but Passed as variable value. This function also has as many parameters as a cow, and I can’t explain them all here. For all the specific parameters, please refer to the document (English): Option Reference

. In addition, the get_settings() function is the same as get_option(). Exactly the same function, get_settings generally appears in some older themes. You can search for get_option in the .php file in the theme directory. You should be able to find many. Here are a few common ones:

get_option('home')

This Used to obtain the URL of the blog homepage. It should be noted that all parameter forms of this function cannot be directly replaced by URLs like bloginfo() above, because it does not directly output the value, but must be replaced by quotation marks. For example: you can replace

echo get_option('home'); with echo 'https://www.ludou.org/';

, if it is

, you can directly replace this code with https://www.ludou.org/, these are some simple PHP Programming method, I believe you can also draw inferences about what is said below. get_option('blogname') Used to get the blog name. get_option('blog_charset')

Used to get the encoding of the page, such as UTF-8.

4. get_bloginfo() blog information function

If I hadn’t written this article, I wouldn’t have known that WordPress has so many functions that can be used to obtain blog information , and they all have similar functions. To be honest, I can’t figure it out! The replacement method of this function is the same as get_option(). I will not go into details here. For detailed parameter description, please see: get_bloginfo()

5. Chinese theme code

Many themes have to consider international applications, so they use translation functions _e() and __(), etc. This can adapt to users in different language areas and facilitate their own production. Language packs, considering that our blogs are all in Chinese, and these contents are all static, we can Chineseize the themes we use, so that WordPress will not retrieve the language packs, and it can also speed up WordPress to a certain extent. speed.

5. Author function

If you are the only one writing your blog, then replace the functions that output author information with your own information. .

   

Used to output the URL of the author archive page, you can replace this function with the following code:

<a href="作者存档页的网址" title="由 XX 发表">作者昵称</a>
Copy after login

用于显示作者的昵称,你可以直接用作者的昵称替换这段代码。

get_the_author();用于获取作者的昵称,你可以用以下内容替换这段代码:&#39;作者昵称&#39;;

用于输出作者的网址URL,跟the_author_posts_link函数功能是不一样的。你可以用以下代码代替这段代码:

<a href="作者网站的网址" title="查看 XX 的站点">作者昵称</a>
Copy after login

六、侧边栏静态化

如果你的侧边栏不需要后台的提供的小工具,或者你喜欢直接用代码来实现侧边栏的某些效果,那么你可以删除sidebar.php中的动态调用小工具的代码,前提是你懂HTML、PHP编程。

<?php if ( !function_exists(&#39;dynamic_sidebar&#39;) || !dynamic_sidebar(&#39;north_sidebar&#39;) ) : ?>***<?php endif; ?>
Copy after login

你可以在***所在的位置添加你自己的侧边栏的代码,添加成功后你可以将

<?php if ( !function_exists(&#39;dynamic_sidebar&#39;) || !dynamic_sidebar(&#39;north_sidebar&#39;) ) : ?>
Copy after login

<?php endif; ?>
Copy after login

删除,这样WordPress就不会去检测你的小工具了,当然你也不能在后台添加小工具了。

七、友情链接静态化

大多数博客的友情链接都是通过后台的小工具或wp_list_bookmarks()函数来输出,这样做的好处是在后台 - 链接那里添加链接前台就会立刻显示,不用你手动地去修改代码等。如果你追求速度,你还可以将友情链接的代码静态化,上面已经说了sidebar.php去除小工具的方法,已经不能用小工具来显示友情链接了。在删除小工具功能之前,先打开你的博客首页查看源代码,找出友情链接部分的代码,如:

<div class="widget widget_links">
	<h3>友情链接</h3>
	<ul>
		<li><a href="http://example/" title="example">example</a></li>
		<li><a href="http://example2/" title="example2">example2</a></li>
	</ul>
</div>
Copy after login

     你可以将这部分代码添加到第六点将到的***部分就可以了。注意:此操作需要你了解HTML,而且每次要修改友情链接的时候需要你手动在sidebar.php中编辑HTML代码。

推荐学习:《WordPress教程

The above is the detailed content of How to make WordPress theme code static? Tutorial sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:ludou.org
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!