ウィジェットを使わない場合
サイドバー (sidebar.php)
…省略
<aside id="archives" class="widget">
<h3 class="widget-title"><?php esc_html_e( 'Archives', 'sparkling' ); ?></h3>
<ul><!-- アーカイブのパラメーターを設定 -->
<?php
$archives_args = array(
'type' => 'monthly',//表示するアーカイブの種類
'format' => 'html', //アーカイブの表示形式
'show_post_count' => true, //投稿数を表示するか
'echo' => 1, //表示するか(1)、値を返すか(0)
'order' => 'DESC', //項目をどの方向に並べるか
);
wp_get_archives( $archives_args );
?>
</ul>
</aside>
か、↓この書き方も可能
<?php wp_get_archives( 'type=monthly&limit=12&show_post_count=1' ); ?>
ウィジェットを使う場合
サイドバーテンプレートの典型的なコード
is_active_sidebarを使ってサイドバー「sidebar-1」に何かウィジェットが入っているか調べて、何か入っていればdynamic_sidebarで表示しています。
// (sidebar.php) <?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?> <div id="secondary" class="widget-area" role="complementary"> <?php dynamic_sidebar( 'sidebar-1' ); ?> </div><!-- #secondary --> <?php endif; ?>
▼functions.phpに定義されたサイドバーウィジェットエリア「sidebar-1」
// (functions.php)
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentytwelve' ),//サイドバーの名称。twentytwelveはテキストドメイン省略可能(theme=twentytwelve)テーマ名
'id' => 'sidebar-1',
'description' => __( 'Appears on posts and pages except the optional Front Page template, which has its own widgets', 'twentytwelve' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
「Main Sidebar」は翻訳ファイルtwentytwelve-ja.poによって「メインサイドバー」と翻訳されます。
結果として、Twenty Twelveで「外観」-「ウィジェット」メニューを開くと「メインサイドバー」が使えるようになっています。