チュートリアルは時代遅れのように見えるので、私はそれに依存しません。WordPress ソースをハッキングする必要はありません。適切なフィルターにフックする単純なプラグインを作成します。
あなたの場合、これらのフィルターはwidget_categories_dropdown_args
(ウィジェットオプションで「ドロップダウンとして表示」を選択した場合)およびwidget_categories_args
(ウィジェットがリストをリンク付きの通常のテキストとして表示する場合)です。
その知識があれば、実際のプラグインをコーディングできます (私はそれを Myplugin と呼んでいます。名前を変更する必要があると思います) - その PHP コードをファイルに入れるだけですwp-content/plugins/myplugin.php
:
<?php
/**
* @package Myplugin
* @version 1.0
*/
/*
Plugin Name: Myplugin
Plugin URI: http://example.com
Description:
Author: You
Version: 1.0
Author URI: http://example.com
*/
// Create a list with the ID's of all children for
// the given category-id
function myplugin_recursive_filter($catid) {
$result = array($catid);
$cats = get_categories(array(
'child_of' => $catid,
));
foreach($cats as $category) {
$result[] = $category->cat_ID;
}
return implode(",", $result);
}
// Actual filter function. Just set the "exclude"
// entry to a comma separated list of category ID's
// to hide.
function myplugin_filter_categories_args($args) {
// 6 is the "tag_ID"
$args['exclude'] = myplugin_recursive_filter(6);
// or hard code the list like that:
//$args['exclude'] = '6,10,11,12';
// but you'd have to include the ID's of the
// children, because "eclude" is not recursive.
return $args;
}
// Register the filter to the relevant tags
add_filter('widget_categories_dropdown_args',
'myplugin_filter_categories_args', 10, 1);
add_filter('widget_categories_args',
'myplugin_filter_categories_args', 10, 1);
-entry は再帰的ではないため、この関数myplugin_recursive_filter
が必要です (ウィジェット オプションで [階層を表示] をオンにしている場合を除く)。exclude
カテゴリがそれほど変わらない場合は、パフォーマンスを向上させるために、関数呼び出しを (子を含む) ID のハードコーディングされたリストに置き換えることができます。