0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

wordpressウィジェットで特定カテゴリを非表示

Posted at

wordpressのウィジェットでカテゴリを表示する際
・親カテゴリのみ非表示にしたい
・あるカテゴリを除外したい
という要望がありました。

widget_categories_argsというフック

widget_categories_argsというフックがあり
このフックでウィジェットの内容を調整できるというものがありました。
https://codex.wordpress.org/Plugin_API/Filter_Reference/widget_categories_args

wp_list_categories()という関数に渡す前に
ウィジェットを調整するフックということでした。

使い方

wp_list_categories()の引数を使える感じで
以下のようにフックに配列を投げると
あとはwp_list_categories()のようにふるまってくれて
ウィジェットを好きに調整できる感じでした。

widget.php

add_filter('widget_categories_args', 'adjustment_category', 10);
function adjustment_category(){
	$cat_args['child_of'] = '2';// 親カテゴリを非表示に
	$cat_args['title_li'] = '';// カテゴリタイトルを非表示に
	return $cat_args;
}

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?