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

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

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