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

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".