メニューを切り替える
Toggle preferences menu
個人設定を切り替える
ログインしていません
編集を行うと、IPアドレスが公開されます。

モジュール:RelatedByCategory

提供:U-Stella Wiki
2025年4月15日 (火) 05:30時点におけるU-Stella (トーク | 投稿記録)による版 (ページの作成:「local p = {} function p.show(frame) local title = mw.title.getCurrentTitle() local categories = title:getParentCategories() local results = {} local limit = tonumber(frame.args[1]) or 5 for catTitle, _ in pairs(categories) do if mw.title.new(catTitle).namespace == 14 then -- Category namespace local cat = mw.title.new(catTitle):getContent() local pages = mw.site.stats.pagesInCategory(catTitle, false)…」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

このモジュールについての説明文ページを モジュール:RelatedByCategory/doc に作成できます

local p = {}

function p.show(frame)
    local title = mw.title.getCurrentTitle()
    local categories = title:getParentCategories()
    local results = {}
    local limit = tonumber(frame.args[1]) or 5

    for catTitle, _ in pairs(categories) do
        if mw.title.new(catTitle).namespace == 14 then -- Category namespace
            local cat = mw.title.new(catTitle):getContent()
            local pages = mw.site.stats.pagesInCategory(catTitle, false)
            for _, page in ipairs(pages) do
                if page.prefixedText ~= title.prefixedText and #results < limit then
                    table.insert(results, '[[' .. page.prefixedText .. ']]')
                end
            end
        end
    end

    if #results == 0 then
        return '<div class="related-footer"><small>関連記事はまだありません。</small></div>'
    end

    return '<div class="related-footer"><strong>📚 関連記事:</strong><ul><li>' .. table.concat(results, '</li><li>') .. '</li></ul></div>'
end

return p