Convert your WordPress theme to HTML5
HTML5 introduces a range of great new features and simple options. Soon it will be fully supported by most browsers used today. Eventually everyone will have to convert their WordPress themes from XHTML to HTML5. After Google’s Panda update, your website needs clearer, easier-to-read code to rank better on Google. I'll teach you how to convert your theme from XHTML to HTML5. We will also take care of the 2% of internet users who have JavaScript disabled (for backwards compatibility).
Our goal
In this tutorial, we will focus on converting a WordPress theme from XHTML to HTML5. We'll walk through the changes step by step through the files listed below (these files are located in your theme folder, i.e. wp-content/themes/yourtheme/here!)
- header.php
- index.php
- sidebar.php:
- footer.php
- single.php(optional)
Basic HTML5 layout
Let's take a look at the basic HTML5 layout we're going to build. HTML5 is more than just a document type at the beginning of the code. Several newly introduced elements help us style and code in an efficient way with less markup (this is one of the reasons why HTML5 is better).
<!DOCTYPE html> <html lang="en"> <head> <title>TITLE | Slogan!</title> </head> <body> <nav role="navigation"></nav> <!--Ending header.php--> <!--Begin index.php--> <section id="content"> <article role="main"> <h1>Title of the Article</h1> <time datetime="YYYY-MM-DD"></time> <p>Some lorem ispum text of your post goes here.</p> <p>The article's text ends.</p> </article> <aside role="sidebar"> <h2>Some Widget in The Sidebar</h2> </aside> </section> <!--Ending index.php--> <!--begin footer.php--> <footer role="foottext"> <small>Some Copyright info</small> </footer> </body> </html>
Now we just need to know the new HTML5 for header
, footer
, nav
, section
and article
Just place the label wherever you want. In contrast to div
structured XHTML, the names of the newly introduced tags are self-explanatory.
Step 1 Convert header.php to HTML5
Now I will show you the code commonly used in header.php of XHTML WordPress themes.
XHTML theme header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My Blog</title> <?php wp_head(); ?> </head> <body> <!-- Header --> <div class="header"> <div class="container"> <h1><a href="<?php bloginfo('url');?>">My Blog is Working Great.</a></h1> </div><!-- End Container --> </div><!-- End Header --> <!-- Navigation Bar Starts --> <div class="navbar"> <div class="container"> <ul class="grid nav_bar"> <?php wp_list_categories('navs_li='); ?> </ul> </div> </div> <!-- Navigation Bar Ends --> <div class="container">
Someone must ask why we do all this? The answer is simple, it is the semantic markup of HTML5. It reduces markup making it very easy to understand and manage.
HTML5 header.php (conversion)
Read the code, then follow the instructions below to convert your theme's header.php to HTML5.
<!doctype html> <html> <head> <title>My Blog</title> <?php wp_head(); ?> </head> <body> <!-- Header --> <header> <div class="container"> <h1 class="grid"><a href="<?php bloginfo('url');?>">My Blog is Working Great.</a></h1> </div> </header> <!-- End Header --> <!-- Navigation Bar Starts--> <nav> <div class="navbar"> <ul class="grid nav_bar"> <?php wp_list_categories('title_li='); ?> </ul> </div> </nav> <!-- Navigation Bar Ends --> <section class="container">
As you can see, the converted code is very similar to the XHTML code. Let's discuss these changes.
-
<!doctype html>
– HTML5 has a very simple doctype, but it contains many new semantic tags -
<header>
– Semantic markup for the header part -
<nav>
– We replaced the navbar’sdiv
code with a new semantic tag for controlling the navbar in HTML5.
Note: Some people include the section
tag in the header. There's a lot of debate about this. I personally object to including the section
tag in the header as it ruins the aesthetics of HTML5. Of course you can use the old div
there.
What about scripts and stylesheets?
When converting a WordPress theme to HTML5, the scripts and stylesheets included in the header are really simple. In HTML5, we only use the <script>
and <link>
tags. So remove type="text/javascript"
- all browsers will treat the <script>
tag as JavaScript unless you explicitly write its type. Likewise, remove the type="text/css"
from the <link>
tag in the stylesheet.
Consider old browsers!
Contains HTML shiv to support older browsers. This is a simple JavaScript file. Some examples of shivs are Remy Sharp's HTML5 script or Modernizr script. Let's use Modernizr.
We need to enqueue the script from our functions.php file like this:
function html5_scripts() { // Register the Modernizr script wp_register_script( 'modernizr', get_template_directory_uri() . '/js/Modernizr-1.6.min.js' ); // Enqueue Modernizr wp_enqueue_script( 'modernizr' ); } add_action( 'wp_enqueue_scripts', 'html5_scripts', 1 );
Tips: Put consecutive title tags into <hgroup>
Note: This script needs to be placed at the very top inside the <?php wp_head();?>
tag, that's why we gave add_action
has priority 1.



Step 2Convert index.php to HTML5
Common XHTML index.php consists of the following tags. I will go through the conversion step by step and explain the entire process after conversion.
NOTE: I won't add the entire code here as it will make the post longer for no reason.
XHTML索引.php
<div id="container"> <div id="content"> <div id="entries"> <div id="post">...</div> </div><!--Ending Entries--> <?php get_sidebar(); ?> </div><!--Ending content--> </div><!--Ending container--> <?php get_footer(); ?>



HTML5 index.php(转换)
<div id="container"> <div id="content"> <section id="entries"> <article id="post">...</article> </section><!--end entries--> <?php get_sidebar(); ?> </div><!--end content--> </div><!--end wrap--> <?php get_footer(); ?>
在看我们所做的更改之前,我们必须知道HTML5为我们提供了三个基本的布局建模标签:Section
、article
和aside
。 Section
将替换条目的 div,article
将替换帖子的 div,稍后 aside
将用于我们的侧边栏。
-
<section>
– HTML5 有一个名为section
的布局标签,用于分隔其中使用的代码块 -
<article>
– 帖子部分的语义标签,类似于section
-
<aside>
– 帖子图像的语义标记,用于将其放在一边和侧边栏 -
面包屑和页面导航
– 如果我们的主题有面包屑,那么它们将在div
中使用,例如<div class="breadcrumbs">...</div>
,对于页面导航,我们将使用<nav id="pgnav"php cngtphpcn...< /nav>



HTML5 中的完整 Index.php
注意: 我粘贴了一个通用的index.php,如下所示,下面是一些转换为 HTML5 的完整代码。
<section class="entries"> <?php if (have_posts()) : while (have_posts()) : the_post(); <article class="post" id="post-<?php the_ID(); ?>"> <aside class="post_image"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { ?> <a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory');?>/images/noImage.gif" title="<?php the_title(); ?>" /></a> <?php }?> </aside> <section class="post_content"> <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> <p><?php echo get_the_excerpt(); ?></p> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>" class="read_more ">Read More</a> </section> <section class="meta"> <p> <?php the_category(',') ?></p> <p><?php the_tags(""); ?></p> </section> </article> <?php endwhile; else: ?> <p> <?php _e('Sorry, no posts matched your criteria.'); ?> </p> <?php endif; ?> <?php posts_nav_link(' ⏼ ', __('« Newer Posts'), __('Older Posts »')); ?> </section>
第 3 步 处理 sidebar.php
我们将在侧边栏中使用 <aside>
而不是 div
,例如:
XHTML 中的 sidebar.php
<div id="sidebar">...</div>
使用<aside>
后变成如下。
HTML5 中的 sidebar.php
<aside id="sidebar">...</aside<
这很简单!



第 4 步 footer.php 编辑
我们将在 footer.php 中使用 <footer>
语义标签而不是简单的 div
,例如:
XHTML 中的 footer.php
<div id="footer"> <div id="foot_widgets">...</div> <div id="copyright">...</div> </div> <?php wp_footer(); ?> </body> </html>



HTML5 中的 footer.php
<footer id="footer"> <section id="foot_widgets">...</section> <section id="foot_widgets">...</section> <section id="foot_widgets">...</section> <div id="copyright">...</div> </footer> <?php wp_footer(); ?> </body> </html>
第 5 步处理 single.php
single.php 没有什么特别的变化,所以我只是粘贴更改后的代码,这可能对一些初学者有帮助。我在其中使用了 section
和 article
标签。如果您愿意,您还可以使用 <time>
标签。
XHTML 中的 single.php
<?php get_header(); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="container"> <div class="breadcrumbs"><?php the_breadcrumb(''); ?></div> <div class="content"> <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> <div id="entry-content-single"> <?php the_content('<p >Read More</p>'); ?> </div> <div class="meta"> Posted by: <?php the_author() ?> <?php edit_post_link(__('Edit This')); ?> <p><?php the_tags(""); ?></p> </div> <div class="clearfix"></div> </div> <!-- End of post --> </div></div> <?php get_footer(); ?>



HTML5 中的 single.php
<?php get_header(); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <section class="content"> <div class="breadcrumbs"><?php the_breadcrumb(''); ?></div> <article class="box"> <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> <section id="entry-content-single"> <?php the_content('<p>Read More</p>'); ?> </section> <section class="meta"> Posted by: <?php the_author() ?> <?php edit_post_link(__('Edit This')); ?> <p><?php the_tags(""); ?></p> </section> <div class="clearfix"></div> </article> <!-- end post --> </section></div> <?php get_footer(); ?>
注意: 关于SEO,有些人在帖子标题之前使用 <header class="entry-header">
,这也是一个很好的做法。
第6步最后是style.css
最终我们关心的是向后兼容性问题。出于对旧版浏览器的安全考虑,HTML5 元素应使用 display: block
样式显示为块。只需将以下代码放在 style.css 的顶部即可:
header, nav, section, article, aside, figure, footer { display: block; }
附加说明
如果您使用嵌入到模板文件中的音频或视频,则必须使用 HTML5 音频和视频元素。可以在下面的备忘单中查看更多标签。每当您添加一些新功能时,请研究一下如何使用其语义标签将其添加到 HTML5 中。
HTML5 资源
- HTML5 备忘单
- HTML5 教程
- HTML5 演示
一些 HTML5 免费主题
- 二十一点
- Yoko
- 15 个主题和框架
现在轮到你了
您要使用 HTML5 吗?您是否已更改为 HTML5?这些更改是否会影响您的 SEO 排名?请在下面的评论中告诉我们!
The above is the detailed content of Convert your WordPress theme to HTML5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.
