Home > CMS Tutorial > WordPress > Convert your WordPress theme to HTML5

Convert your WordPress theme to HTML5

WBOY
Release: 2023-09-06 09:33:09
Original
1245 people have browsed it

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>
Copy after login

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">
Copy after login

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">
Copy after login

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’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.

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 );
Copy after login

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.

将您的 WordPress 主题转换为 HTML5Convert your WordPress theme to HTML5将您的 WordPress 主题转换为 HTML5

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(); ?>
Copy after login
将您的 WordPress 主题转换为 HTML5Convert your WordPress theme to HTML5将您的 WordPress 主题转换为 HTML5

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(); ?>
Copy after login

在看我们所做的更改之前,我们必须知道HTML5为我们提供了三个基本的布局建模标签:SectionarticleasideSection 将替换条目的 div,article 将替换帖子的 div,稍后 aside 将用于我们的侧边栏。

  • <section> – HTML5 有一个名为 section 的布局标签,用于分隔其中使用的代码块
  • <article> – 帖子部分的语义标签,类似于 section
  • <aside> – 帖子图像的语义标记,用于将其放在一边和侧边栏
  • 面包屑和页面导航 – 如果我们的主题有面包屑,那么它们将在 div 中使用,例如 <div class="breadcrumbs">...</div> ,对于页面导航,我们将使用 <nav id="pgnav"php cngtphpcn...< /nav>
将您的 WordPress 主题转换为 HTML5Convert your WordPress theme to HTML5将您的 WordPress 主题转换为 HTML5

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>
Copy after login

第 3 步 处理 sidebar.php

我们将在侧边栏中使用 <aside> 而不是 div,例如:

XHTML 中的 sidebar.php

<div id="sidebar">...</div>
Copy after login

使用<aside>后变成如下。

HTML5 中的 sidebar.php

<aside id="sidebar">...</aside<
Copy after login

这很简单!

将您的 WordPress 主题转换为 HTML5Convert your WordPress theme to HTML5将您的 WordPress 主题转换为 HTML5

第 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>
Copy after login
将您的 WordPress 主题转换为 HTML5Convert your WordPress theme to HTML5将您的 WordPress 主题转换为 HTML5

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>
Copy after login

第 5 步处理 single.php

single.php 没有什么特别的变化,所以我只是粘贴更改后的代码,这可能对一些初学者有帮助。我在其中使用了 sectionarticle 标签。如果您愿意,您还可以使用 <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(); ?>
Copy after login
将您的 WordPress 主题转换为 HTML5Convert your WordPress theme to HTML5将您的 WordPress 主题转换为 HTML5

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(); ?>
Copy after login

注意: 关于SEO,有些人在帖子标题之前使用 <header class="entry-header"> ,这也是一个很好的做法。


第6步最后是style.css

最终我们关心的是向后兼容性问题。出于对旧版浏览器的安全考虑,HTML5 元素应使用 display: block 样式显示为块。只需将以下代码放在 style.css 的顶部即可:

header, nav, section, article, aside, figure, footer { display: block; }
Copy after login

附加说明

如果您使用嵌入到模板文件中的音频或视频,则必须使用 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!

source:php.cn
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