Snippet: Dynamic page title

If you are a programmer and want to change page title dynamically then here is the snippet for you.

Add following code to “functions.php”

Snippet

function page_title() {
	if (is_home()) {
		  _e('Home', 'theme');
	  } elseif (is_archive()) {
		$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
		if ($term) {
		  echo $term->name;
		} elseif (is_post_type_archive()) {
		  echo get_queried_object()->labels->name;
		} elseif (is_day()) {
		  printf(__('Daily Archives: %s', 'theme'), get_the_date());
		} elseif (is_month()) {
		  printf(__('Monthly Archives: %s', 'theme'), get_the_date('F Y'));
		} elseif (is_year()) {
		  printf(__('Yearly Archives: %s', 'theme'), get_the_date('Y'));
		} elseif (is_author()) {
		  global $post;
		  $author_id = $post->post_author;
		  printf(__('Author Archives: %s', 'theme'), get_the_author_meta('display_name', $author_id));
		} else {
		  single_cat_title();
		}
	  } elseif (is_search()) {
		printf(__('Search Results for %s', 'theme'), get_search_query());
	  } elseif (is_404()) {
		_e('File Not Found', 'theme');
	  } else {
		the_title();
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>