Skip to content

Instantly share code, notes, and snippets.

@katzueno
Last active July 14, 2016 10:58
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save katzueno/8eae37d0579f195a7be30865fbe6eb9b to your computer and use it in GitHub Desktop.

How to customize page title in theme file of concrete5

Instruction

Copy and paste (override) the following code where Loader::element('header_required'); is.

header_1.php

Page Title Format:

  • Page Title - Site name
  • Page Title | Page Title of 1st page - Site name

Since concrete5.7.5.7, the site name supports multilingual. Register your site name in Engish at [Dashboard] - [System & Setting] - [Basics] - [Site Name]. And then, use [Dashboard] - [System and Setting] - [Multilingual] - [Translation Interface] to enter the multilingual site name.

Page Description & Page Keyword

You can define your page description and page keyword programmatically. My sample code will try to use the programmatically defined $pageDescrition and $pageMetaKeywords, but if the admin already set Meta Description or Collection Descriton, then concrete5 will try to use those first.

header_2.php

Page Title Format:

  • Page Title - Site name
  • Page Title | Page Title of Parent page - Site name

cocnrete5 でページタイトルをカスタマイズする方法

使い方

次のコードを、 concrete5 のテーマファイルで Loader::element('header_required'); が記述されている行に貼り付けてください。

header_1.php

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

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

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

Meta Description と Meta Keyword

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

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

header_2.php

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

  • トップ & 第1階層ページ: [ページタイトル] - [サイト名]
  • 第2階層以降のページ: [ページタイトル] | [親ページタイトル] - [サイト名]
<?php
$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 : '',
)
);
<?php
$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) {
$deptC = array_shift($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 : '',
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment