Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Dynamic Title Tag

<title>
   <?php 
      if (function_exists('is_tag') && is_tag()) { 
         single_tag_title("Tag Archive for &quot;"); echo '&quot; - '; } 
      elseif (is_archive()) { 
         wp_title(''); echo ' Archive - '; } 
      elseif (is_search()) { 
         echo 'Search for &quot;'.wp_specialchars($s).'&quot; - '; } 
      elseif (!(is_404()) && (is_single()) || (is_page())) { 
         wp_title(''); echo ' - '; } 
      elseif (is_404()) { 
         echo 'Not Found - '; } 
      if (is_home()) { 
         bloginfo('name'); echo ' - '; bloginfo('description'); } 
      else {
          bloginfo('name'); }
      if ($paged>1) { 
         echo ' - page '. $paged; } 
   ?>
</title>

Reference URL

Comments

  1. TeMc
    Permalink to comment#

    if you want the site-title to appear first, this would do:

    [title]
       [?php
          if (is_home()) {
             bloginfo('name'); echo ' - '; bloginfo('description'); }
          else {
              bloginfo('name'); echo ' - '; }
          if (function_exists('is_tag') && is_tag()) {
             single_tag_title("Tag Archive for ""); echo '"'; }
          elseif (is_archive()) {
             wp_title(''); echo ' Archive'; }
          elseif (is_search()) {
             echo 'Search for "'.wp_specialchars($s).'"'; }
          elseif (!(is_404()) && (is_single()) || (is_page())) {
             wp_title(''); }
          elseif (is_404()) {
             echo 'Not Found'; }
          if ($paged>1) {
             echo ' - page '. $paged; }
    ?]
    [/title]

    (replace [html] and [php ?> with <html> <php ?>)

  2. Ant

    1. Not very clean code, it could be better.
    2. isn’t wp_title(); function enough?

  3. John Phanchalad
    Permalink to comment#

    Can you please share about how to make dynamic heading tag (h1,h2) for WordPress theme? I usually used free WordPress theme at wordpress.org and It’s hard to find a great free theme. Thanks mate.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

```
<script>
  function example() {
    element.innerHTML = "<div>code</div>";
  }
</script>
```