LoginSignup
19
18

More than 5 years have passed since last update.

【WordPress】複数のサイドウィジェットを定義する

Posted at

管理画面>外観>ウィジェットでサイドウィジェットを作れる、というのは多分みなさんご存知だと思います。
投稿のアーカイブやカテゴリ、タグクラウドなんかをパパッと表示してくれて、とても便利です。
そのウィジェットですが、複数個作りたいときってありますよね。

作り方は簡単で、ウィジェットを登録するときに、

functions.php
if(function_exists('register_sidebar')) {
    register_sidebar();
}

て、してたのを

functions.php
if(function_exists('register_sidebars')) {
    register_sidebars(3, array('name' => 'サイドウィジェット%d'));
}

にするだけです。
「3」となっているのはウィジェットを3つ作りたいからです。作りたい個数を書いてください。
%dとなっている部分は、WordPress側で処理して、1から番号をふってくれます。
ウィジェット

呼び出すときは、dynamic_sidebar()で呼び出していたのを、番号をつけて呼び出します。

dynamic_sidebar(2);

本家ドキュメントを見ると、エラーを防ぐために返り値を使いなさい的なことが書いてあります。

sidebar.php
<!-- サイドナビ -->
    <nav id="side_navi">
        <ul>
        <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar(2)): ?>
                <li>ウィジェットが動的に作成されませんでした</li>
            <?php endif; ?>
        </ul>
    </nav>

参考サイト
本家
[WordPress]ウィジェットを複数設定する方法

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18