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!
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:
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">
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 fileheader.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:echo get_option('home'); with echo 'https://www.ludou.org/';
, if it is php echo get_option('home'); ?>, 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')
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()
<a href="作者存档页的网址" title="由 XX 发表">作者昵称</a>
用于显示作者的昵称,你可以直接用作者的昵称替换这段代码。
get_the_author();用于获取作者的昵称,你可以用以下内容替换这段代码:'作者昵称';
用于输出作者的网址URL,跟the_author_posts_link函数功能是不一样的。你可以用以下代码代替这段代码:
<a href="作者网站的网址" title="查看 XX 的站点">作者昵称</a>
如果你的侧边栏不需要后台的提供的小工具,或者你喜欢直接用代码来实现侧边栏的某些效果,那么你可以删除sidebar.php中的动态调用小工具的代码,前提是你懂HTML、PHP编程。
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('north_sidebar') ) : ?>***<?php endif; ?>
你可以在***所在的位置添加你自己的侧边栏的代码,添加成功后你可以将
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('north_sidebar') ) : ?>
和
<?php endif; ?>
删除,这样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>
你可以将这部分代码添加到第六点将到的***部分就可以了。注意:此操作需要你了解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!