LoginSignup
0
0

More than 5 years have passed since last update.

【WordPress・プラグインなし】ページタイプごとにタイトルを変更する方法

Last updated at Posted at 2019-03-23
functions.php
function change_document_title_parts( $title_parts ){
  // デフォルトタイトルをリセット
  $title_parts['title'] = '';
  $title_parts['tagline'] = '';
  $title_parts['site'] = '';

  $site_name = trim( get_bloginfo('name') );
  if(is_front_page()): //フロントページ
    $title_parts['title'] = trim( get_the_title() );
    $title_parts['site'] = $site_name;
  elseif(is_singular()): //投稿ページ
    $title_parts['title'] = trim( get_the_title() );
    $title_parts['site'] = $site_name;
  elseif(is_archive()): //アーカイブページ
    $title_parts['title'] = '「'.$title_parts['title'].'」のアーカイブ';
  elseif(is_search()): //検索結果ページ
    $title_parts['title'] = $title_parts['title'];
    $title_parts['site'] = $site_name;
  elseif(is_404()): //404
    $title_parts['title'] = 'お探しのページは見つかりませんでした';
    $title_parts['site'] = $site_name;
  endif;

  return $title_parts;
}
add_filter( 'document_title_parts', 'change_document_title_parts' );
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0