title 要素のカスタマイズ

タイトルフォーマットの定義

デフォルトでは、title 要素の表記は、「サイト名 :: ページ名」というフォーマットになっています。詳しくは、当サイト「設定ファイルの使い方」ページの「SEO関連の設定」の項を参考にしてください。

SEOヘルパーの操作

title に表示する内容の要素や表示フォーマットは、SEOヘルパーと呼ばれるクラスで管理されています。このクラスを使えば、フォーマットの設定以外にも、様々な操作が行えます。

例:タイトルセグメントのクリア

application/bootstrap/app.php への記載例

Events::addListener('on_before_render', function($event) {
    /** @var \Concrete\Core\Html\Service\Seo $seo */
    $seo = Core::make('helper/seo');
    $seo->clearTitleSegments();
});

ページリストブロックで、トピックやタグで絞り込みする際に、トピック名がタイトルに自動的に追加されますが、それらの追加要素を消去します。

例:ページ送りの番号をタイトルに追加

application/bootstrap/app.php への記載例

Events::addListener('on_before_render', function($event) use ($app) {
    /** @var \Concrete\Core\Html\Service\Seo $seo */
    $seo = $app->make('helper/seo');

    /** @var \Concrete\Core\Page\PageList $list */
    $list = new \Concrete\Core\Page\PageList();
    $parameter = $list->getQueryPaginationPageParameter();

    /** @var \Symfony\Component\HttpFoundation\ParameterBag $query */
    $query = \Concrete\Core\Http\Request::getInstance()->query;
    foreach ($query->all() as $key => $value) {
        if (strpos($key, $parameter) === 0) {
            $seo->addTitleSegment(sprintf('ページ %d', $value));
            break;
        }
    }
});

デフォルトではページ送りをしても title 要素に変化はありませんが、ページ送りをした際に title に「ページ 2」と追加するカスタマイズの例です。

SEOヘルパーをオーバーライド

SEOヘルパーは \Concrete\Core\Html\Service\Seo クラスとして実装されていますが、オーバーライドして差し替えれば完全に異なる挙動にすることができます。これにより、より柔軟にタイトルの表記をカスタマイズできます。

application/bootstrap/app.php への記載例

Core::bind('helper/seo', 'Application\Src\Html\Service\Seo');

テーマからページタイトルをカスタマイズする方法

テーマファイルの header_required を呼び出すところにタイトルの変数を渡すことによってテーマファイルからページタイトルをカスタマイズできます。なお、この方法は Meta Descriotion と Meta Keyword でも同じことが可能です。

使い方

次のコードを、 concrete5 のテーマファイルで Loader::element('header_required'); が記述されている行の前にページタイトルを設定するプログラムを書いてください。

ページタイトルのフォーマット

  • トップ & 第1階層ページ: [ページタイトル] - [サイト名]
  • 第2階層以降のページ: [ページタイトル] | [第1階層のページタイトル] - [サイト名]

concrete5.7.5.7 より、サイト名が多言語対応になりました。[管理画面] - [システムと設定] - [基本] - [サイト名] は英語のサイト名を記入してください。日本語を含むその他の言語のサイト名は、[管理画面] - [システムと設定] - [多言語] - [翻訳インターフェース]で行えます。

Meta Description と Meta Keyword

Meta Description と Meta Keywords をプログラム的に指定できます。 サンプルコードでは $pageDescrition と $pageMetaKeywords がこのコードの前に設定されていたらそれを使うというサンプルです。

サイト管理者が通常のように Meta 属性や、ページ説明を入力していれば、それが優先されるというようにしています。

    $metaTitle = $c->getCollectionAttributeValue('meta_title');
    if ($metaTitle) $pageTitle = $metaTitle;
 
    if (!$pageTitle)
    {
    	$siteName = h(\Config::get('concrete.site'));
        $collentionName = $c->getCollectionName();

    	$isRoot = null;
    	$trail = $nh->getTrailToCollection($c);
    	if (is_array($trail) && count($trail) > 1) {
    	    $count = count($trail);
    	    array_splice($trail, $count - 1);
    	    $deptC = array_pop($trail);
    	    $deptName = h($deptC->getCollectionName());
    	} else {
    	    $isRoot = true;
    	}        	
    	if ($isRoot) {
    		$pageTitle = $collentionName . ' - ' . $siteName;
    	} else {
    		$pageTitle = $collentionName . ' | ' . $deptName . ' - ' . $siteName;
    	}
    }
    
    $thisPageDescription = $c->getAttribute('meta_description');
    if (!$thisPageDescription) $thisPageDescription = $c->getCollectionDescription();
    if ($thisPageDescription) $pageDescription = $thisPageDescription;

    $thisPageMetaKeywords = $c->getAttribute('meta_keywords');
    if ($thisPageMetaKeywords) $pageMetaKeywords = $thisPageMetaKeywords;

    Loader::element('header_required',
        array(
            'pageTitle' => $pageTitle,
            'pageDescription' => isset($pageDescription) ? $pageDescription : '',
            'pageMetaKeywords' => isset($pageMetaKeywords) ? $pageMetaKeywords : '',
        )
    );