WordPress タイトルの変更

公開日: : PHP, WordPress

wordpress タイトルの変更

テンプレートタグ the_title(); get_the_title();

WordPress でコンテンツ中のタイトルを変更するには、ループの中で使用する the_title() か、ループの外では get_the_title() を使います。

the_title(前に置くテキスト,後ろに置くテキスト,表示・取得の真偽値);

// 現在の投稿のタイトルを表示(引数は全て省略可)
the_title();

// h3タグ で囲ってタイトルを表示
the_title( '<h3>','</h3>' ); (第3引数デフォルトは true でタイトルを表示)

// 現在の投稿のタイトルを取得(第3引数 false で取得)
$title = the_title( '','' , false);

// the_title() をフックしてタイトルを変更する
function my_the_title( $title ) {
    $title = '変更後のタイトル';
    return $title;
}
add_filter( 'the_title', 'my_the_title' );

get_the_title( 投稿IDもしくは投稿特定オブジェクト);

// 投稿IDを指定しない場合は、現在の投稿のタイトルを返す
$title = get_the_title();

// 投稿ID が 3 のタイトルを取得する
$title = get_the_title(3);

// 親のページタイトルを取得する
$parent_id = $post->post_parent;
if ($parent_id) {
    $parent = get_post($parent_id);
    $parent_title = get_the_title( $parent->post_parent );
}

titleタグの変更 add_theme_support( ‘title-tag’ )

titleタグ のタイトルを変更したい場合、WordPress4.4以降書き方が変わりました。

非推奨 wp_title();

以前は、header.php に以下のようなコードを追加していました。

<title><?php wp_title( '|', true, 'right' ); ?><?php bloginfo( 'name' ); ?></title>

このやり方は、WordPress4.4以降では非推奨となっています。

4.4以降 add_theme_support( ‘title-tag’ );

WordPress4.4以降でタイトルを出力するには、functions.php に以下のコードを追加し、add_theme_support を有効化します。

function setup_theme() {
  add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'setup_theme' );

有効化したら、同じくfunction.php に pre_get_document_title や document_title_parts などをフックして、タイトルをカスタマイズします。

titleタグのタイトルを変更する

function my_pre_get_document_title( $title ) {
  $title = '変更後のタイトル';
  return $title;
}
add_filter( 'pre_get_document_title', 'my_pre_get_document_title' );

titleタグのタイトルをパーツごとに変更する

function my_document_title_parts( $title ){
  $title['title']   = '変更後のタイトル';
  $title['page']    = '変更後のページが分割されている場合のページ番号';
  $title['tagline'] = '変更後のフロントページのキャッチコピー';
  $title['site']    = '変更後のフロントページ以外のサイト名';
  return $title;
}
add_filter( 'document_title_parts', 'my_document_title_parts' );

titleタグのセパレータを変更する

function my_document_title_separator($sep) {
  $sep = '|';
  return $sep;
}
add_filter( 'document_title_separator', 'my_document_title_separator' );

関連記事

MVCの典型的な相関図

PHPフレームワークについて

フレームワークとは? 開発する際に頻繁に必要とされる汎用的な機能をまとめて提供している、アプリケー

記事を読む

WordPress の PHP をちょっと見てみよう Ⅱ

WordPress の PHP をちょっと見てみよう Ⅱ

前記事の続きで、wp-settings.php の68行目あたりから見ていきます。 <今回のピ

記事を読む

WordPress Twenty Seventeen function.php

WordPress twentyseventeen の function.php を見る – その2

前回の続きです。 次のスターターコンテンツは、今回のシンプルテンプレートには不要ですが、よくわ

記事を読む

Wordpress 自作フォーム (チェックボックスなど)

WordPress 自作フォーム その3(チェックボックスなど)

以前、WordPress 自作フォーム その1の記事で Wordpress で自作フォームを作ってみ

記事を読む

ロリポップでWordPressのPHPバージョンを5から7にする

ロリポップでWordPressのPHPバージョン7.1に変更後「サイトに技術的な問題が発生しています。」

WordPress.org の推奨環境 PHP7以上 MySQL5.6以上またはMaria

記事を読む

WordPress レスポンシブ テンプレート 元にサイトを作ってみる その1

「レスポンシブ テンプレート の メニューやブログの設置」の記事では、メニューやブログの設置例をやっ

記事を読む

WordPress カレンダー カスタマイズ

WordPress カレンダー カスタマイズ(add_filter版)

前回、「WordPress の PHP をちょっと見てみよう Ⅲ」でフックをやったので、カレンダーで

記事を読む

Wordpress VR Test

WordPress で VR させて、360°のパノラマ画像を表示する

WordPress.com内ショートコードで VR させる WordPress.com内で、VR(

記事を読む

All-in-One WP Migration で サーバ移動

「All-in-One WP Migration」プラグインで 簡単に WordPress のサーバー移行する手順メモ

1.移行元サイトでのデータエクスポート 「All-in-One WP Migration(公式リン

記事を読む

WordPress エラー:Failed to load plugin: table from url https://cdn.tinymce.com/4/plugins/table/plugin.min.js

WordPress エラー:Failed to load plugin: table from url https://cdn.tinymce.com/4/plugins/table/plugin.min.js

WordPress エラーの状態 WordPress 管理画面のテキスト入力欄で(ビジュアルタブの

記事を読む

PHP WordPress

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WordPress エラー:Failed to load plugin: table from url https://cdn.tinymce.com/4/plugins/table/plugin.min.js
WordPress エラー:Failed to load plugin: table from url https://cdn.tinymce.com/4/plugins/table/plugin.min.js

WordPress エラーの状態 WordPress 管理画面のテキ

wordpress タイトルの変更
WordPress タイトルの変更

テンプレートタグ the_title(); get_the_title

All-in-One WP Migration で サーバ移動
「All-in-One WP Migration」プラグインで 簡単に WordPress のサーバー移行する手順メモ

1.移行元サイトでのデータエクスポート 「All-in-One WP

DNSサーバーとは
ドメインとは?DNSサーバーとは?

ドメインとは? ドメインとは? ドメインとは、インターネット上のネ

javascriptで複数同じ名前のformの値を取得する
javascriptで複数同じ名前のformの値を取得するとエラー Cannot read property ‘value’ of undefined

Javascript で value の値を取得する このようなHT

→もっと見る

    • 202404
      Mon Tue Wed Thu Fri Sat Sun
      1234567
      891011121314
      15161718192021
      22232425262728
      2930
    にほんブログ村 IT技術ブログへ にほんブログ村 IT技術ブログ PHPへ にほんブログ村 IT技術ブログ WordPressへ
    にほんブログ村 FC2 Blog Ranking
    PAGE TOP ↑