如何在 MediaWiki 中实现随机页面按钮?

How can I implement a random page button in MediaWiki?

我通过将以下代码添加到 Template.php 皮肤文件来制作随机页面按钮。

Html::rawElement( 'a', [ 
    'id' => 'random',
    'class' => 'random',
    'href' => './index.php?title=Special:Random'
] )

但我不喜欢 'href' 部分。有没有办法写成'href' => $this->data['nav_urls']['mainpage']['href']?

我不确定,您到底想用它做什么,但是,将静态 linking 替换为 index.php?title=Special:Random 最简单的部分是使用标题 object 并让它为你生成一个 link。在你的情况下是这样的:

SpecialPage::getTitleFor( 'Random' )->getLinkURL()

在您的完整示例中:

Html::rawElement( 'a', [ 
    'id' => 'random',
    'class' => 'random',
    'href' => SpecialPage::getTitleFor( 'Random' )->getLinkURL()
] );