Last Updated: February 25, 2016
·
1.072K
· andrejcremoznik

Keep semantics by wrapping the title in H1 when it makes sense

Sometimes it makes sense to start your page with the company title in H1 tag. But in case of a single blog post, having that post's title in H1 and the company title in DIV would make much more sense.

We can use Wordpress conditional tags to achieve this.

From header.php:

<?php
/**
 * Page title in H1 when it makes sense, DIV otherwise
 */
$logo_tag = 'div';
if(is_front_page()) {
    $logo_tag = 'h1';
}
?>

<header>
    <<?php echo $logo_tag; ?> id="logo">
        <a href="<?php echo home_url("/"); ?>" rel="home"><?php bloginfo('name'); ?></a>
    </<?php echo $logo_tag; ?>>
</header>