Code Snippet

Home » Code Snippets » WordPress » Dynamic Title Tag

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

Subscribe to The Thread

  1. TeMc

    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. 1. Not very clean code, it could be better.
    2. isn’t wp_title(); function enough?

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~