[WordPress]ウィジェットで特定の親カテゴリーのドロップダウンを表示する方法

スポンサーリンク

1. functions.phpにショートコードの定義を追加

function getCategoryCombobox($args)
{
  $categoryId = 33; // 表示する親カテゴリーID
  $categories = get_categories(array('taxonomy' => 'category', 'child_of' => $categoryId, 'pad_counts' => true));
  $cat_selct = '<select onchange="document.location.href=this.options[this.selectedIndex].value;">';
  $cat_selct .= '<option></option>';
  foreach ( $categories as $category ) {
    $cat_selct .= '<option value="'.esc_html( get_category_link( $category->term_id ) ).'">'.($category->parent===$categoryId?'':' ').esc_html( $category->name . "($category->count)").'</option>';
  }
  $cat_selct .= '</select>';
  echo $cat_selct;
}
add_shortcode('categoryCombobox', 'getCategoryCombobox');

2. ウィジェット⇒カスタムHTML

カスタムHTMLにショートコードを記述する。

コメント