Home >CMS Tutorial >WordPress >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).
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!)
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.
Now I will show you the code commonly used in header.php of XHTML WordPress themes.
<!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.
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.
aba7b36f87decd50b18c7e3e3c150106
– HTML5 has a very simple doctype, but it contains many new semantic tags1aa9e5d373740b65a0cc8f0a02150c53
– Semantic markup for the header partc787b9a589a3ece771e842a6176cf8e9
– We replaced the navbar’s div
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.
When converting a WordPress theme to HTML5, the scripts and stylesheets included in the header are really simple. In HTML5, we only use the 3f1c4e4b6b16bbbd69b2ee476dc4f83a
and 2cdf5bf648cf2f33323966d7f58a7f3f
tags. So remove type="text/javascript"
- all browsers will treat the 3f1c4e4b6b16bbbd69b2ee476dc4f83a
tag as JavaScript unless you explicitly write its type. Likewise, remove the type="text/css"
from the 2cdf5bf648cf2f33323966d7f58a7f3f
tag in the stylesheet.
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 d8eccd9ed644b68a6460a2bb84548c82
Note: This script needs to be placed at the very top inside the 737230fd7c6a60ef945718e17906c65d
tag, that's why we gave add_action
has priority 1.
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.
<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(); ?>
<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
将用于我们的侧边栏。
2f8332c8dcfd5c7dec030a070bf652c3
– HTML5 有一个名为 section
的布局标签,用于分隔其中使用的代码块23c3de37f2f9ebcb477c4a90aac6fffd
– 帖子部分的语义标签,类似于 section
15221ee8cba27fc1d7a26c47a001eb9b
– 帖子图像的语义标记,用于将其放在一边和侧边栏面包屑和页面导航
– 如果我们的主题有面包屑,那么它们将在 div
中使用,例如 26af29c551fce3a276c8c520c5161cd1...16b28748ea4df4d9c2150843fecfba68
,对于页面导航,我们将使用 a8d116a310d40d10e32a84202dd15709
注意: 我粘贴了一个通用的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>
我们将在侧边栏中使用 15221ee8cba27fc1d7a26c47a001eb9b
而不是 div
,例如:
<div id="sidebar">...</div>
使用15221ee8cba27fc1d7a26c47a001eb9b
后变成如下。
<aside id="sidebar">...</aside<
这很简单!
我们将在 footer.php 中使用 c37f8231a37e88427e62669260f0074d
语义标签而不是简单的 div
,例如:
<div id="footer"> <div id="foot_widgets">...</div> <div id="copyright">...</div> </div> <?php wp_footer(); ?> </body> </html>
<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>
single.php 没有什么特别的变化,所以我只是粘贴更改后的代码,这可能对一些初学者有帮助。我在其中使用了 section
和 article
标签。如果您愿意,您还可以使用 46dd80ba616c57a652514755c74c4211
标签。
<?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(); ?>
<?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,有些人在帖子标题之前使用 3534bfbbdb2128f14bde5b82a4f4d904
,这也是一个很好的做法。
最终我们关心的是向后兼容性问题。出于对旧版浏览器的安全考虑,HTML5 元素应使用 display: block
样式显示为块。只需将以下代码放在 style.css 的顶部即可:
header, nav, section, article, aside, figure, footer { display: block; }
如果您使用嵌入到模板文件中的音频或视频,则必须使用 HTML5 音频和视频元素。可以在下面的备忘单中查看更多标签。每当您添加一些新功能时,请研究一下如何使用其语义标签将其添加到 HTML5 中。
您要使用 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!