Cms/mediawiki/h1タイトルをパンクズ化

提供: 初心者エンジニアの簡易メモ
2026年4月28日 (火) 16:20時点におけるAdmin (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

バージョン1.45

LocalSettings.php

$wgHooks['BeforePageDisplay'][] = function ( $out, $skin ) {

    $out->addScriptFile( '/resources/custom-title.js' );

    return true;
};

resources/custom-title.js

"resources/custom-title.js" 20L, 470B
// パンくずリンク
document.addEventListener("DOMContentLoaded", function () {
    const h1 = document.querySelector("#firstHeading");
    if (!h1) return;
    const text = h1.textContent;
    const parts = text.split("/");
    let path = [];
    const html = parts.map(part => {
        path.push(part);
        const encoded = path.join("/");
        return `<a href="/index.php/${encoded}">${part}</a>`;
    });
    h1.innerHTML = html.join("/");
});
<?pre>
==バージョン1.24==
スラッシュ(/)で区切られたページごとにリンクを貼ります。

*skins/Vector/VectorTemplate.php

変更前
<pre>
?>"><span dir="auto"><?php $this->html( 'title' ) ?></span></h1>

変更後

<span dir="auto">
<?php if ($_GET['action']): ?>
<?php $this->html( 'title' ) ?>
<?php else: ?>
<?php $name = $this->get( 'title' ); $names = explode('/', $name); $path = ""; ?>
<?php foreach ($names as $key => $name): ?><?php $path .= $name; ?><a href="/index.php/<?php echo htmlspecialchars($path); ?>"><?php echo htmlspecialchars($name); ?></a><?php if ($key + 1 < count($names)): ?>/<?php endif; ?><?php $path .= "/"; ?><?php endforeach; ?>
<?php endif; ?>
</span></h1>