ウィジェットを複数使用する
通常ウィジェットはサイドバーで使われる場合が多いと思いますが、
サイドバー以外の複数の場所でウィジェットを使用することができます。
例として、sidebar.php、single.phpでウィジェットを使用するとします。
対応させるには、functions.php、ウィジェットを表示させたいテンプレートを編集します。
functions.php
<?php if ( function_exists('register_sidebar') ) { register_sidebar(array( 'name' => 'side', 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', )); register_sidebar(array( 'name' => 'single', 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', )); } ?>
‘name’=>’ ‘, で任意の名前にできます。
そこで付けた名前を dynamic_sidebar(‘ ‘) の部分に記述します。
後はウィジェットを表示させたい場所に、下記のようにコードを記述します。
sidebar.php
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('side') ) : ?> <?php endif; ?>
single.php
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('single') ) : ?> <?php endif; ?>
タグ: functions.php, ウィジェット
記事公開日:
最終更新日:
[…] ウィジェットを複数使用する | WP MEMO […]